我有以下JSON数组存储在一个名为fieldsArray的变量:
[
{
"Header - Goods Receipt":[
{"warehouse":{"editableField":"false"}},
{"documentNo":{"editableField":"true"}}
]
},
{
"Lines - Goods Receipt":[
{"description":{"editableField":"false"}}
]
}
]
我需要循环遍历这个数组,抓取"Header - Goods Receipt"首先,然后向下钻取它的子元素("warehouse")和"documentNo")以及它们嵌套的"editableField"的值孩子的钥匙。到目前为止,我已经设法抓住"标题-货物收据",但不能得到它的孩子。到目前为止,我已经完成了以下操作:
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");
}
}
}
得到以下错误:
error: cannot find symbol
Iterator<String> iteratorTwo = currentParentKeyProperties.keys();
symbol: method keys()
和
error: cannot find symbol
String isEditable = currentParentKeyProperties.getJSONObject(currentChildKey).getString("editableField");
method: getJSONObject(String)
注意:currentParentKeyProperties给出以下JSON数组:
[
{
"warehouse":{"editableField":"false"}
},
{
"documentNo":{"editableField":"true"}
}
]
全新的Java -不知道为什么getJSONObject不工作?我在代码的其他地方使用过它。提前谢谢。
您的问题与Java无关,您只是忘记了一个步骤。
String currentParentKeyProperties =fieldsObject.optString (currentParentKey);
是不正确的,你想要的是得到一个JSONObject。