0

I wannt to access the fields in my JSONArray. The nested brackets inside the JSONArray is very troublesome. I can't see how this format is an acceptable JSONArray return value. I get an JSONException when I try to access a field (eg "rethink3__Address__c") using getJSONObject().

 [
   [
      {
         "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
      }
   ]
]
Jonathan Gagne
  • 4,241
  • 5
  • 18
  • 30
I Like
  • 1,711
  • 2
  • 27
  • 52
  • `http://jsonviewer.stack.hu`, use this website to view how your JSON is structured. it's very helpful for me at least. – anuraagdjain Sep 23 '18 at 01:40

1 Answers1

1

A [] = json array and {} = json object. So try this.

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);
})
Jonathan Gagne
  • 4,241
  • 5
  • 18
  • 30
  • 1
    Thanks this is the right idea. I accomplished same thing with java like below `for (int i=0;i – I Like Sep 23 '18 at 20:44