[英]Nested JSONArray
I need to read a JSON file with following form:我需要读取具有以下形式的 JSON 文件:
{
"Data":[{
"foo":"22",
"bar":"33",
"array":[
{
"1foo":"22",
"1bar":"33"
},
{
"2foo":"22",
"2bar":"33",
}
],
"anotherData":{
"foofoo":"22",
"barbar":"33"
},
"some more data":"11",
"some more data":"11"
},
{and the cycle here starts again from -->
"foo":"22",
"bar":"33",
"array":[
My question stands: How do I access individual elements given it's sometimes JSONObject and sometimes JSONArray.我的问题是:我如何访问单个元素,因为它有时是 JSONObject,有时是 JSONArray。 I tried using org.json library but I'm failing to access anything after this line -> "array":[.我尝试使用 org.json 库,但我无法访问此行之后的任何内容 -> “array”:[。 I tried various combinations of JSONObject and JSONArray up to no avail.我尝试了 JSONObject 和 JSONArray 的各种组合,但都无济于事。
My latest code looked something like this:我最新的代码看起来像这样:
List<Data> data= new ArrayList<>();
String rawJson = getJsonAsString();
JSONObject outer = new JSONObject(rawJson);
JSONArray jArr= outer.getJSONArray("Data");
JSONObject inner= outer.getJSONObject("array");
for(int i =0; i<jArr.length(); i++){
JSONObject jsonEvent = jArr.getJSONObject(i);
String foo = jsonEvent.getString("foo"); <-- this works,
String 1foo = jsonEvent.getString("1foo"); <-- but this doesn't and i cant access it
I tried dozens of different solutions(tried myself and tried to find something here as well, but every case with those nested arrays is different and I can't add those solutions together to get answer for my case)我尝试了几十种不同的解决方案(自己尝试并尝试在这里找到一些东西,但是嵌套 arrays 的每个案例都是不同的,我无法将这些解决方案加在一起以获得我的案例的答案)
You can increase your readability if you beautify your raw JSON string:如果美化原始 JSON 字符串,则可以提高可读性:
{
"Data":[
{
"foo":"22",
"bar":"33",
"array":[
{
"1foo":"22",
"1bar":"33"
},
{
"2foo":"22",
"2bar":"33"
}
],
"anotherData":{
"foofoo":"22",
"barbar":"33"
},
"some more data":"11",
"some more data":"11"
},
{ and the cycle here starts again from -->
"foo":"22",
"bar":"33",
"array":[
...
],
...
}
]
}
So, stick to the following code:因此,坚持使用以下代码:
JSONArray jArr = outer.getJSONArray("Data");
jArr
is now filled with array of your Object. jArr
现在充满了你的 Object 的数组。
And to loop through your Object array, you can use a for-loop which you have done it correctly.要遍历您的 Object 数组,您可以使用您已正确完成的for 循环。
for (int i = 0; i < jArr.length(); i++) {
// The below gets your Object
JSONObject jsonEvent = jArr.getJSONObject(i);
}
And now each jsonEvent
contains your Object, which you can access the Object by their type.现在每个jsonEvent
包含你的 Object,你可以通过它们的类型访问 Object。
For example, if you would like to access foo
, you can use:例如,如果你想访问foo
,你可以使用:
String foo = jsonEvent.getString("foo"); // foo = "22"
String bar = jsonEvent.getString("bar"); // bar = "33"
// Note that your array is another Array, you need to get it as JSONArray first
JSONArray arrayJson = jsonEvent.getJSONArray("array");
And as 1foo
is in the first Object for your array
, you need to access it like this:由于1foo
位于array
的第一个 Object 中,因此您需要像这样访问它:
JSONObject firstObjectInArray = arrayJson.getJSONObject(0);
String target = firstObjectInArray.getString("1foo"); // target = "22"
And to access foofoo
, as it is an attribute of the JSON Object anotherData
, so you should obtain anotherData
as JSONObject
first, and then you can access foofoo
:而要访问foofoo
,因为它是JSON Object anotherData
的一个属性,所以你应该先获取anotherData
作为JSONObject
,然后你就可以访问foofoo
:
JSONObject anotherDataObject = jsonEvent.getJSONObject("anotherData");
String foofoo = anotherDataObject.getString("foofoo"); // foofoo = "22"
So the full code within your for-loop should look like this:因此,您的for 循环中的完整代码应如下所示:
for (int i = 0; i < jArr.length(); i++) {
// The below gets your Object
JSONObject jsonEvent = jArr.getJSONObject(i);
String foo = jsonEvent.getString("foo");
JSONArray arrayJson = jsonEvent.getJSONArray("array");
String target = arrayJson.getJSONObject(0).getString("1foo");
// Add to get foofoo
JSONObject anotherDataObject = jsonEvent.getJSONObject("anotherData");
String foofoo = anotherDataObject.getString("foofoo");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.