{
"transaction": {
"id": 1,
"empid": "12345",
"details1": {
"name": "xyz",
"age": "30",
"sex": "M",
"Address": {
"Office": "office",
"Home": "Home"
}
},
"abcDetails": "asdf",
"mobile": 123455
},
"details2": {
"id": 2,
"empid": "64848",
"details": {
"name": "eryje",
"age": 3027,
"sex": "M",
"Address": {
"Office": "office",
"Home": "Home"
}
},
"abcDetails": "fhkdl",
"mobile": 389928
}
}
我正在获取上述格式的数据。在这里,我使用循环分割和迭代数据。第一次得到以下格式化数据。因此,在这里我想获取姓名和年龄值以及 details1.Address.Office
值(键不是静态的)。
"details1": {
"name": "xyz",
"age": "30",
"sex": "M",
"Address": {
"Office": "office",
"Home": "Home"
}
}
最佳答案
尝试使用 JSONObject keys() 获取 key ,然后迭代每个 key 以获取动态值。
// searchResult refers to the current element in the array "search_result"
JSONObject questionMark = searchResult.getJSONObject("question_mark");
Iterator keys = questionMark.keys();
while(keys.hasNext()) {
// loop to get the dynamic key
String currentDynamicKey = (String)keys.next();
// get the value of the dynamic key
JSONObject currentDynamicValue = questionMark.getJSONObject(currentDynamicKey);
// do something here with the value...
}
关于java - 使用 Java 进行嵌套 JSON 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42324704/