使用 jQuery 和 Javascript 解析本地 JSON 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17944344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:14:46  来源:igfitidea点击:

Parse local JSON file with jQuery and Javascript

javascriptjqueryjsonparsing

提问by lucgian84

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

我正在尝试解析位于我的计算机上的 JSON 文件。我想解析它。JSON 文件具有以下结构:

{
  "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:

在我的 html 中,我导入了 jQuery 库,并创建了一个在页面加载时加载的函数。代码如下:

<!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

现在我在 firebug 控制台上看到它可以正确读取 JSON 文件,但我不知道如何解析这个 JSON。我在谷歌搜索,但我发现了很多使用远程 JSON 的例子。你能帮我理解如何解析本地 JSON 文件吗?谢谢

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

PS:注意我在这里发布的网站是为移动浏览器制作的。

回答by Quentin

getJSONwill parse it for you.

getJSON会为你解析。

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)).

只需删除该var obj = $.parseJSON(json);行(因为它将对对象进行字符串化并尝试将其解析为 JSON(它不会是))。

回答by Sharad

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

我认为您不需要解析 json。由于您使用的是 $.getJSON(),它会自动解析 json。