I have the following JSON Array stored inside a variable called fieldsArray:
[
{
"Header - Goods Receipt":[
{"warehouse":{"editableField":"false"}},
{"documentNo":{"editableField":"true"}}
]
},
{
"Lines - Goods Receipt":[
{"description":{"editableField":"false"}}
]
}
]
I need to loop through this array, grabbing the "Header - Goods Receipt" first, and then drill down and get it's children ("warehouse" and "documentNo") and the values for their nested "editableField" child keys. So far, I have managed to grab "Header - Goods Receipt", but cannot get to it's children. I have done the following so far:
for (int i = 0; i < fieldsArray.length(); i ) {
JSONObject fieldsObject = fieldsArray.optJSONObject(i);
Iterator<String> iterator = fieldsObject.keys();
while(iterator.hasNext()) {
String currentParentKey = iterator.next();
String currentParentKeyProperties = fieldsObject.optString(currentParentKey);
//I have tried the below code to get to "warehouse" and "documentNo", but it's not working:
Iterator<String> iteratorTwo = currentParentKeyProperties.keys();
while(iteratorTwo.hasNext()) {
String currentChildKey = iteratorTwo.next();
String isEditable = currentParentKeyProperties.getJSONObject(currentChildKey).getString("editableField");
}
}
}
I get the following errors:
error: cannot find symbol
Iterator<String> iteratorTwo = currentParentKeyProperties.keys();
symbol: method keys()
and
error: cannot find symbol
String isEditable = currentParentKeyProperties.getJSONObject(currentChildKey).getString("editableField");
method: getJSONObject(String)
NOTE: currentParentKeyProperties gives the following JSON Array:
[
{
"warehouse":{"editableField":"false"}
},
{
"documentNo":{"editableField":"true"}
}
]
Brand new to Java - not sure why getJSONObject is not working? I have used it elsewhere in my code. Thanks in advance.
CodePudding user response:
Your problem is not related to Java, you just forgot a step.
String currentParentKeyProperties = fieldsObject.optString(currentParentKey);
is not correct, what you want is to get a JSONObject.