gpt4 book ai didi

java - 如何在java中解析嵌套的json数组

转载 作者:行者123 更新时间:2023-11-30 03:20:29 26 4
gpt4 key购买 nike

我想用Java解析netsted json:下面是json字符串:

[
{
"id": 1,
"empid": "12345",
"details": {
"name": "xyz",
"age": "30",
"sex": "M",
"Address": {
"Office": "office",
"Home": "Home"
}
},
"abcDetails": "asdf",
"mobile": 123455
},
{
"id": 2,
"empid": "64848",
"details": {
"name": "eryje",
"age": 3027,
"sex": "M",
"Address": {
"Office": "office",
"Home": "Home"
}
},
"abcDetails": "fhkdl",
"mobile": 389928
}
]

我需要上面json中的姓名、年龄详细信息,有人可以帮助如何在java中解析这些值,我尝试了下面的代码来获取这些值 - 看起来它是嵌套的,不知道如何获取这些值。

        JSONArray jsonarray = new JSONArray(str);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject obj1 = jsonarray.getJSONObject(i);

String name = obj1.getString("name");
String age = obj1.getString("age");

System.out.println(name);
System.out.println(age);
}

最佳答案

您可能需要使用#getJSONObject()来获取嵌套对象。

  • 但是,这是我个人的印象,为什么org.json包的版本字符串是日期格式?可能这不是一个好的图书馆......

示例:

package testing;

import org.json.JSONArray;
import org.json.JSONObject;

/**
* Hello world!
*/
public class App {

static String json = ""
+ "["
+ " {"
+ " \"id\": 1,"
+ " \"empid\": \"12345\","
+ " \"details\": {"
+ " \"name\": \"xyz\","
+ " \"age\": \"30\","
+ " \"sex\": \"M\","
+ " \"Address\": {"
+ " \"Office\": \"office\","
+ " \"Home\": \"Home\""
+ " }"
+ " },"
+ " \"abcDetails\": \"asdf\","
+ " \"mobile\": 123455"
+ " },"
+ " {"
+ " \"id\": 2,"
+ " \"empid\": \"64848\","
+ " \"details\": {"
+ " \"name\": \"eryje\","
+ " \"age\": 3027,"
+ " \"sex\": \"M\","
+ " \"Address\": {"
+ " \"Office\": \"office\","
+ " \"Home\": \"Home\""
+ " }"
+ " },"
+ " \"abcDetails\": \"fhkdl\","
+ " \"mobile\": 389928"
+ " }"
+ "]";

public static void main(String[] args) throws Exception {

JSONArray jsonarray = new JSONArray(json);
System.out.println(String.format("JSONArray length => %d", jsonarray.length()));

for (int i = 0; i < jsonarray.length(); i++) {
JSONObject obj1 = jsonarray.getJSONObject(i);
JSONObject details = obj1.getJSONObject("details");
System.out.println(String.format("details => %s", details.toString()));

String name = details.getString("name");
int age = details.getInt("age");
System.out.println(name);
System.out.println(age);
}
}
}

结果:

$ mvn exec:java -Dexec.mainClass="testing.App"
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testing 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ testing >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ testing <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ testing ---
JSONArray length => 2
details => {"sex":"M","Address":{"Home":"Home","Office":"office"},"age":"30","name":"xyz"}
xyz
30
details => {"sex":"M","Address":{"Home":"Home","Office":"office"},"age":3027,"name":"eryje"}
eryje
3027
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.576s
[INFO] Finished at: Fri Jul 17 10:46:29 JST 2015
[INFO] Final Memory: 7M/106M
[INFO] ------------------------------------------------------------------------

关于java - 如何在java中解析嵌套的json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31467037/

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