【问题标题】:How to format JSONArray with nested arrays如何使用嵌套数组格式化 JSONArray
【发布时间】:2019-02-26 22:57:48
【问题描述】:

我想访问 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
      }
   ]
]

【问题讨论】:

  • http://jsonviewer.stack.hu,使用此网站查看您的 JSON 结构。至少对我很有帮助。

标签: arrays json object multidimensional-array jsonexception


【解决方案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);
})

【讨论】:

  • 谢谢这是正确的想法。我用java完成了同样的事情,如下for (int i=0;i<clean.length();i++){ JSONArray y = clean.getJSONArray(i); x = y.getJSONObject(0); cleaner.put(x); }
猜你喜欢
  • 2019-04-10
  • 1970-01-01
  • 1970-01-01
  • 2013-06-04
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 2020-09-22
相关资源
最近更新 更多