【问题标题】:Parse JSON with multiple levels解析具有多个级别的 JSON
【发布时间】:2013-04-11 12:25:00
【问题描述】:

我正在尝试解析已分配给 javascript 变量的 JSON,但由于它具有多个级别,我很难循环遍历它

<script type="text/javascript">

        var varJson = {
            "d": {
                "results": [
                    {
                        "__metadata": {
                            "id": "http://www.bradinsight.com/ALFAPI/AlfWebApi/Offices(100013449)",
                            "uri": "http://www.bradinsight.com/ALFAPI/AlfWebApi/Offices(100013449)",
                            "type": "AlfWebApiInfrastructure.Poco.Office"
                        },
                        "Agency": {
                            "__deferred": {
                                "uri": "http://www.bradinsight.com/ALFAPI/AlfWebApi/Offices(100013449)/Agency"
                            }
                        },
                        "Advertiser": {
                            "__deferred": {
                                "uri": "http://www.bradinsight.com/ALFAPI/AlfWebApi/Offices(100013449)/Advertiser"
                            }
                        },
                        "Contacts": {
                            "results": [
                                {
                                    "__metadata": {
                                        "id": "http://www.bradinsight.com/ALFAPI/AlfWebApi/Contacts(id=59980,jobId=1000105450)",
                                        "uri": "http://www.bradinsight.com/ALFAPI/AlfWebApi/Contacts(id=59980,jobId=1000105450)",
                                        "type": "AlfWebApiInfrastructure.Poco.Contact"
                                    },
                                    "id": 59980,
                                    "jobId": 1000105450,
                                    "mask": 67108863,
                                    "name": "Name",
                                    "jobtitle": "Job Title",
                                    "email": "email",
                                    "companyType": "companyType",
                                    "companyId": 1787,
                                    "officeId": 100013449,
                                    "address1": "address1",
                                    "address2": "",
                                    "address3": "",
                                    "address4": "",
                                    "addressTown": "addressTown",
                                    "addressPostCode": "addressPostCode",
                                    "tel": "tel",
                                    "fax": "fax",
                                    "responsibilities": "responsibilities"
                                },
                                {
                                    "__metadata": {
                                        "id": "http://www.bradinsight.com/ALFAPI/AlfWebApi/Contacts(id=64085,jobId=1000105448)",
                                        "uri": "http://www.bradinsight.com/ALFAPI/AlfWebApi/Contacts(id=64085,jobId=1000105448)",
                                        "type": "AlfWebApiInfrastructure.Poco.Contact"
                                    },
                                    "id": 64085,
                                    "jobId": 1000105448,
                                    "mask": 67108863,
                                    "name": "name",
                                    "jobtitle": "jobtitle",
                                    "email": "email",
                                    "companyType": "companyType",
                                    "companyId": 1787,
                                    "officeId": 100013449,
                                    "address1": "address1",
                                    "address2": "",
                                    "address3": "",
                                    "address4": "",
                                    "addressTown": "addressTown",
                                    "addressPostCode": "addressPostCode",
                                    "tel": "tel",
                                    "fax": "fax",
                                    "responsibilities": "responsibilities"
                                }
                            ]
                        },
                        "id": 100013449,
                        "companyId": 1787,
                        "addressLine1": "addressLine1",
                        "addressLine2": "",
                        "addressLine3": "",
                        "addressLine4": "",
                        "addressLine5": "addressLine5",
                        "postCode": "postCode",
                        "regionId": "regionId",
                        "countyId": "countyId",
                        "tvAreaId": "L",
                        "telephone": "telephone",
                        "fax": "fax8",
                        "countryId": "countryId",
                        "email": "email",
                        "isdn": null,
                        "mask": 66060287
                    }
                ]
            }
        };
        
               

        $(document).ready(function () {


            //var parsed = JSON.parse(varJson);

            alert(varJson);

        

        });

    </script>

我试过用这个:

$.each(varJson, function (index, item) {
              alert(item);

                });

但它只是在我的警报中说对象。

【问题讨论】:

  • 那不是 JSON,它是一个 JavaScript“对象字面量”。它已经“解析”了——我想你的意思是“遍历”。
  • @Alnitak 抱歉,我的意思是遍历,因为我想在警报或控制台中查看对象文字的内容。

标签: javascript jquery json


【解决方案1】:

试试这个,

function traverse(jsonObj) {
  if( typeof jsonObj == "object" ) {
    $.each(jsonObj, function(k,v) {
        traverse(v);
    });
  }
  else {
    alert(jsonObj);
  }
}

$(document).ready(function () {
   traverse(varJson);
});

http://jsfiddle.net/satpalsingh/hXEr8/

参考Traverse all the Nodes of a JSON Object Tree with JavaScript

【讨论】:

  • 谢谢!这就是我一直在寻找的。​​span>
  • @nickgowdy 您应该真正接受控制台的强大功能。警报是如此的 1980
【解决方案2】:

你不需要在你的例子中解析它。

使用console.log 而不是alert,您将在您的网络检查器中找到该对象。


如果你确实想alert 做这样的事情

alert(varJson.d.results[0].__metadata.id)

【讨论】:

  • 当我使用 $.each 遍历我的 JSON 时,我得到 LOG: [object Object]。这是我使用 $.each(varJson, function (index, item) { console.log(item); });
  • 你用的是什么浏览器?无需在 chrome 或 firebug 中循环
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多