2017-05-30 5 views
-1

Also habe ich eine ziemlich große JSON-Zeichenfolge, die eine ziemlich fortgeschrittene Struktur (zumindest für mich) hat, die ich mit Javascript analysieren möchte. Hier ist eine leserfreundliche Version davon (das ist ein Beispiel):Eine große JSON-Zeichenfolge analysieren

{ 
    "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" 
      }, 
     } 
    ] 
    } 
} 

Was ich wirklich erhalten und analysieren wollen, ist, wenn Sie einen Blick an ihm nehmen wollen, diese Zeichenfolge (enthält 7 " Eintrag "):

{"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]"}}]}} 

meinen Code so analysieren sie

<div id="liste"></div> 
<script> 
     var objet = JSON.parse('***[....Big string....]***'); 
     document.getElementById("liste").innerHTML = objet.firstName; 
</script> 

ist Also versuchte ich viele Kombinationen wie objet.entries.entry.firstName oder objet.entries [1] (die gibt mir Uncaught TypeError: Cannot read property '1' of undefined) ... Aber ich weiß immer noch nicht, wie ich mein Ziel erreichen kann, zum Beispiel nur .firtName zu bekommen. Und ich finde nur einfache Beispiele für JSONparse() im Internet wie Strings wie diese {"firstname":"Jesper","surname":"Aaberg","phone":"555-0100"} so ..

Wenn jemand weiß, danke im Voraus!

Antwort

1

So I tried a lot of combinations such as objet.entries.entry.firstName or objet.entries[1] (which gives me Uncaught TypeError: Cannot read property '1' of undefined)

Das erste Element eines JavaScript-Array ist das Element am Index 0 - so man wollte einfach:

var firstName = objet.list.entries[0].entry.firstName 
+0

Das gibt mir tatsächlich den gleichen Fehler, änderte ich 'document.getElementById ("liste") .innerHTML = objet.entries [0] .entry.firstName; 'und ich bekomme immer noch das' Uncaught TypeError: Kann Eigenschaft '0' von undefined nicht lesen ' – JackA

+0

Ok danke, du hattest Recht, es war nicht die Struktur ich Ich habe "Liste" hinzugefügt, und es hat funktioniert: 'objet.list.entries [0] .entry.firstName' Aber, nur um sicher zu sein, in zukünftigen Code, mit dem Beispiel, das ich gab, sollte ich nicht "Liste" hinzufügen? – JackA

+0

@JackA Ich glaube, ich habe die 'liste' in deinem json wirklich vermisst, nicht du. Sorry, dass – Jamiec

Verwandte Themen