所以我有一個相當大的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