所以我有一个相当大的JSON字符串,它有一个相当先进的结构(至少对我来说),我想用javascript解析。下面是它的读者的版本(这是一个例子):解析一个大的JSON字符串
{
"list": {
"pagination": {
"count": 0,
"hasMoreItems": true,
"totalItems": 0,
"skipCount": 0,
"maxItems": 0
},
"entries": [
{
"entry": {
"id": "string",
"firstName": "string",
"lastName": "string",
"description": "string",
"avatarId": "string",
"email": "string",
"skypeId": "string",
"googleId": "string",
"instantMessageId": "string",
"jobTitle": "string",
"location": "string",
"company": {
"organization": "string",
"address1": "string",
"address2": "string",
"address3": "string",
"postcode": "string",
"telephone": "string",
"fax": "string",
"email": "string"
},
}
]
}
}
我真正得到和要分析是,如果你想看看它,这个字符串(其中包含7“条目“):
{"list":{"pagination":{"count":7,"hasMoreItems":false,"totalItems":7,"skipCount":0,"maxItems":100},"entries":[{"entry":{"lastName":"Beecher","userStatus":"Helping to design the look and feel of the new web site","jobTitle":"Graphic Designer","statusUpdatedAt":"2011-02-15T20:20:13.432+0000","mobile":"0112211001100","emailNotificationsEnabled":true,"description":"Alice is a demo user for the sample Alfresco Team site.","telephone":"0112211001100","enabled":false,"firstName":"Alice","skypeId":"abeecher","avatarId":"198500fc-1e99-4f5f-8926-248cea433366","location":"Tilbury, UK","company":{"organization":"Moresby, Garland and Wedge","address1":"200 Butterwick Street","address2":"Tilbury","address3":"UK","postcode":"ALF1 SAM1"},"id":"abeecher","email":"[email protected]"}},{"entry":{"firstName":"Administrator","emailNotificationsEnabled":true,"company":{},"id":"admin","enabled":true,"email":"[email protected]"}},{"entry":{"firstName":"Alex","lastName":"lol","emailNotificationsEnabled":true,"company":{},"id":"alexandra","enabled":true,"email":"[email protected]"}},{"entry":{"firstName":"Guest","emailNotificationsEnabled":true,"company":{},"id":"guest","enabled":false}},{"entry":{"firstName":"Jack","lastName":"lol","emailNotificationsEnabled":true,"company":{},"id":"jack","enabled":true,"email":"[email protected]"}},{"entry":{"lastName":"Jackson","userStatus":"Working on a new web design for the corporate site","jobTitle":"Web Site Manager","statusUpdatedAt":"2011-02-15T20:13:09.649+0000","mobile":"012211331100","emailNotificationsEnabled":true,"description":"Mike is a demo user for the sample Alfresco Team site.","telephone":"012211331100","enabled":false,"firstName":"Mike","skypeId":"mjackson","avatarId":"3fbde500-298b-4e80-ae50-e65a5cbc2c4d","location":"Threepwood, UK","company":{"organization":"Green Energy","address1":"100 Cavendish Street","address2":"Threepwood","address3":"UK","postcode":"ALF1 SAM1"},"id":"mjackson","email":"[email protected]"}},{"entry":{"firstName":"Nicolas","lastName":"lol","emailNotificationsEnabled":true,"company":{},"id":"nicolas","enabled":true,"email":"[email protected]"}}]}}
所以我的代码来解析它是
<div id="liste"></div>
<script>
var objet = JSON.parse('***[....Big string....]***');
document.getElementById("liste").innerHTML = objet.firstName;
</script>
所以我尝试了很多组合,如objet.entries.entry.firstName或objet.entries的[1](其中给我Uncaught TypeError: Cannot read property '1' of undefined
)... 但我仍然不知道如何达到我的目标,例如获取.firtName。而我只找到互联网上的JSONparse()的简单例子,例如像这样的字符串{"firstname":"Jesper","surname":"Aaberg","phone":"555-0100"}
so ..
如果有人知道提前致谢!
这实际上给了我同样的错误,我改'的document.getElementById(“清单当然” ).innerHTML = objet.entries [0] .entry.firstName;'我仍然得到'未捕获的TypeError:无法读取属性'0'未定义的' – JackA
好的,谢谢,你是对的,它不是结构预计,我添加了“list”,它的工作原理是:'objet.list.entries [0] .entry.firstName'但是,为了确保在将来的代码中,我给出的例子不应该添加“list”? – JackA
@JackA我想我实际上错过了json中的'list',而不是你。对不起, – Jamiec