最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Parse local JSON file with jQuery and Javascript - Stack Overflow

programmeradmin2浏览0评论

I'm trying to parse a JSON file located on my computer. I want to parse it. The JSON file has this structure:

{
  "sites": {
    "site": [
      {
        "id": "01",
        "name": "Sito 1",
        "src": "localhost/root/coupon/sito1",
        "expiryDate": "29 Ago 2013"
      },
      {
        "id": "02",
        "name": "Sito 2",
        "src": "localhost/root/coupon/sito2",
        "expiryDate": "30 Ago 2013"
      },
      {
        "id": "Sito 3",
        "name": "Sito 3",
        "src": "localhost/root/coupon/sito2",
        "expiryDate": "31 Ago 2013"
      }
    ]
  }
}

In my html I import the jQuery library and I made a function that will load when the page is loaded. The code is below:

<!DOCTYPE html>
<html lang="it">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>  
        <title>Lista coupon</title>
        <script type="text/javascript" src="jquery-1.9.1.min.js"></script>
        <script type="text/javascript" charset="utf-8">
            function loadJson() {
                window.alert("Carico il contenuto del file JSON per popolare la lista");
                $(document).ready(function()
                    {
                        $.getJSON('data.json', function(json) {
                            console.log(json);
                        });
                    });
                }
        </script>
    </head>
    <body onload="loadJson();">
        <div id="header">
            <h1>Lista coupon salvati</h1>
        </div>
        <div id="content">
            <p>Di seguito trovi tutte le promozioni salvate</p>

        </div>
        <div id="footer">

        </div>
    </body>
</html>

Now I saw on firebug console that it can read correctly the JSON file, but I don't know how to parse this JSON. I searched in google, but I found a lot of example that use a remote JSON. Can you help me to understand how to parse a local JSON file? Thank you

PS: note the site I post on here it's made for mobile browser.

I'm trying to parse a JSON file located on my computer. I want to parse it. The JSON file has this structure:

{
  "sites": {
    "site": [
      {
        "id": "01",
        "name": "Sito 1",
        "src": "localhost/root/coupon/sito1",
        "expiryDate": "29 Ago 2013"
      },
      {
        "id": "02",
        "name": "Sito 2",
        "src": "localhost/root/coupon/sito2",
        "expiryDate": "30 Ago 2013"
      },
      {
        "id": "Sito 3",
        "name": "Sito 3",
        "src": "localhost/root/coupon/sito2",
        "expiryDate": "31 Ago 2013"
      }
    ]
  }
}

In my html I import the jQuery library and I made a function that will load when the page is loaded. The code is below:

<!DOCTYPE html>
<html lang="it">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>  
        <title>Lista coupon</title>
        <script type="text/javascript" src="jquery-1.9.1.min.js"></script>
        <script type="text/javascript" charset="utf-8">
            function loadJson() {
                window.alert("Carico il contenuto del file JSON per popolare la lista");
                $(document).ready(function()
                    {
                        $.getJSON('data.json', function(json) {
                            console.log(json);
                        });
                    });
                }
        </script>
    </head>
    <body onload="loadJson();">
        <div id="header">
            <h1>Lista coupon salvati</h1>
        </div>
        <div id="content">
            <p>Di seguito trovi tutte le promozioni salvate</p>

        </div>
        <div id="footer">

        </div>
    </body>
</html>

Now I saw on firebug console that it can read correctly the JSON file, but I don't know how to parse this JSON. I searched in google, but I found a lot of example that use a remote JSON. Can you help me to understand how to parse a local JSON file? Thank you

PS: note the site I post on here it's made for mobile browser.

Share Improve this question asked Jul 30, 2013 at 10:19 lucgian84lucgian84 8434 gold badges11 silver badges29 bronze badges 2
  • getJson makes a HTTP call to the server not to your computer. Read the documentation api.jquery.com/jQuery.getJSON – Liam Commented Jul 30, 2013 at 10:21
  • do a console.log on the 'obj'-variable and check how it's built. Will make you understand how you should use it. – ninja Commented Jul 30, 2013 at 10:21
Add a comment  | 

2 Answers 2

Reset to default 12

getJSON will parse it for you.

Just remove the var obj = $.parseJSON(json); line (since that will stringify the object and try to parse it as JSON (which it won't be)).

I don't think you need to parse the json. It will automatically parse the json since you are using $.getJSON().

发布评论

评论列表(0)

  1. 暂无评论