我正在尝试解析我已分配给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": "Mr Tom George",
"jobtitle": "Chairman UK & Northern Europe",
"email": "tom.george@mecglobal.com",
"companyType": "Agency",
"companyId": 1787,
"officeId": 100013449,
"address1": "1 Paris Garden",
"address2": "",
"address3": "",
"address4": "",
"addressTown": "LONDON",
"addressPostCode": "SE1 8NU",
"tel": "020 7803 2000",
"fax": "020 7803 2018",
"responsibilities": "Chairman or President"
},
{
"__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": "Mr Steve Hatch",
"jobtitle": "Chief Executive Officer",
"email": "steve.hatch@mecglobal.com",
"companyType": "Agency",
"companyId": 1787,
"officeId": 100013449,
"address1": "1 Paris Garden",
"address2": "",
"address3": "",
"address4": "",
"addressTown": "LONDON",
"addressPostCode": "SE1 8NU",
"tel": "020 7803 2000",
"fax": "020 7803 2018",
"responsibilities": "Chief Executive or Managing Director"
}
]
},
"id": 100013449,
"companyId": 1787,
"addressLine1": "1 Paris Garden",
"addressLine2": "",
"addressLine3": "",
"addressLine4": "",
"addressLine5": "LONDON",
"postCode": "SE1 8NU",
"regionId": "GL",
"countyId": "LN",
"tvAreaId": "L",
"telephone": "020 7803 2000",
"fax": "020 7803 2018",
"countryId": "GB",
"email": "firstname.surname@mecglobal.com",
"isdn": null,
"mask": 66060287
}
]
}
};
$(document).ready(function () {
//var parsed = JSON.parse(varJson);
alert(varJson);
});
</script>
我试过用这个:
$.each(varJson, function (index, item) {
alert(item);
});
但它只是在警报中说对象。
答案 0 :(得分:2)
试试这个,
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
答案 1 :(得分:1)
您不需要在示例中解析它。
使用console.log
代替alert
,您将成为网络检查员中的对象。
如果你想alert
做这样的事情
alert(varJson.d.results[0].__metadata.id)