gpt4 book ai didi

java - 使用 Java 进行嵌套 JSON 解析

转载 作者:行者123 更新时间:2023-12-01 08:52:05 28 4
gpt4 key购买 nike

{
"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...
}

引用:How to parse a dynamic JSON key in a Nested JSON result?

关于java - 使用 Java 进行嵌套 JSON 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42324704/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com