解析一个大的JSON字符串

时间:2017-05-30 13:00:50

标签: javascript json

所以我有一个非常大的JSON字符串,它有一个非常高级的结构(至少对我而言),我想用javascript解析。这是一个读者友好的版本(这是一个例子):

   {
  "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":"abeecher@example.com"}},{"entry":{"firstName":"Administrator","emailNotificationsEnabled":true,"company":{},"id":"admin","enabled":true,"email":"admin@alfresco.com"}},{"entry":{"firstName":"Alex","lastName":"lol","emailNotificationsEnabled":true,"company":{},"id":"alexandra","enabled":true,"email":"alexandra.lol@test.com"}},{"entry":{"firstName":"Guest","emailNotificationsEnabled":true,"company":{},"id":"guest","enabled":false}},{"entry":{"firstName":"Jack","lastName":"lol","emailNotificationsEnabled":true,"company":{},"id":"jack","enabled":true,"email":"jack.lol@test.com"}},{"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":"mjackson@example.com"}},{"entry":{"firstName":"Nicolas","lastName":"lol","emailNotificationsEnabled":true,"company":{},"id":"nicolas","enabled":true,"email":"nicolas.lol@test.com"}}]}}

所以解析它的代码是

<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"}这样的字符串...

如果有人事先知道谢谢!

1 个答案:

答案 0 :(得分:1)

  

所以我尝试了很多组合,例如objet.entries.entry.firstName或objet.entries [1](这给了我Uncaught TypeError:无法读取属性&#39; 1&#39; of undefined)

javascript数组的第一个元素是索引0的元素 - 所以你只想要:

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