如何使用嵌套数组格式化JSONArray

时间:2018-09-23 01:35:51

标签: arrays json object multidimensional-array jsonexception

我希望访问JSONArray中的字段。 JSONArray内的嵌套括号非常麻烦。我看不到这种格式如何是可接受的JSONArray返回值。尝试使用getJSONObject()访问字段(例如“ rethink3__Address__c”)时,出现JSONException。

 [
   [
      {
         "attributes":{
            "type":"rethink3__Listing__c",
            "url":"\/services\/data\/v42.0\/sobjects\/rethink3__Listing__c\/a06m0000005OPb9AAG"
         },
         "rethink3__Address__c":null,
         "Alarm_Code__c":null,
         "rethink3__Bathrooms__c":0,
         "rethink3__Bedrooms__c":0,
         "rethink3__Size__c":0,
         "Lock_Box_Code__c":null,
         "Lock_Box_Location_Notes__c":null,
         "_soupEntryId":1,
         "_soupLastModifiedDate":1537657104801
      }
   ],
   [
      {
         "attributes":{
            "type":"rethink3__Listing__c",
            "url":"\/services\/data\/v42.0\/sobjects\/rethink3__Listing__c\/a06m0000005OPb9AAG"
         },
         "rethink3__Address__c":null,
         "Alarm_Code__c":null,
         "rethink3__Bathrooms__c":0,
         "rethink3__Bedrooms__c":0,
         "rethink3__Size__c":0,
         "Lock_Box_Code__c":null,
         "Lock_Box_Location_Notes__c":null,
         "_soupEntryId":1,
         "_soupLastModifiedDate":1537657104801
      }
   ]
]

1 个答案:

答案 0 :(得分:1)

一个[] = json数组和{} = json对象。所以试试看。

let myArray = [
    [
        {
            "attributes":{
                "type":"rethink3__Listing__c",
                "url":"\/services\/data\/v42.0\/sobjects\/rethink3__Listing__c\/a06m0000005OPb9AAG"
            },
            "rethink3__Address__c":null,
            "Alarm_Code__c":null,
            "rethink3__Bathrooms__c":0,
            "rethink3__Bedrooms__c":0,
            "rethink3__Size__c":0,
            "Lock_Box_Code__c":null,
            "Lock_Box_Location_Notes__c":null,
            "_soupEntryId":1,
            "_soupLastModifiedDate":1537657104801
        }
    ],
    [
        {
            "attributes":{
                "type":"rethink3__Listing__c",
                "url":"\/services\/data\/v42.0\/sobjects\/rethink3__Listing__c\/a06m0000005OPb9AAG"
            },
            "rethink3__Address__c":null,
            "Alarm_Code__c":null,
            "rethink3__Bathrooms__c":0,
            "rethink3__Bedrooms__c":0,
            "rethink3__Size__c":0,
            "Lock_Box_Code__c":null,
            "Lock_Box_Location_Notes__c":null,
            "_soupEntryId":1,
            "_soupLastModifiedDate":1537657104801
        }
    ]
];

myArray.forEach((myNestedArray)=>{
    let obj = myNestedArray[0]
    console.log(obj.attributes.type);
    console.log(obj._soupLastModifiedDate);
})