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

时间:2015-07-17 01:19:09

标签: java json

我想在Java中解析netsted json:下面是json string:

[
    {
        "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);
        }

4 个答案:

答案 0 :(得分:1)

属性名称和年龄在详细信息中 试试这段代码

JSONArray jsonarray = new JSONArray(str);
        for (int i = 0; i < jsonarray.length(); i++) {
            JSONObject obj1 = jsonarray.getJSONObject(i);
            JSONObject details = obj1.getJSONObject("details");
            String name = details.getString("name");
            String age = details.getString("age");

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

答案 1 :(得分:0)

您可能需要使用#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] ------------------------------------------------------------------------

答案 2 :(得分:0)

在你的代码中你正在做

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

namedetail的子元素,因此您需要先获取detail个对象。

JSONObject detail = obj1.getJSONObject("details");

然后

String name = detail.getString("name");

答案 3 :(得分:0)

//import java.util.ArrayList;
//import org.bson.Document;

Document root= Document.parse("{\"records\":[\n" +
"    {\n" +
"        \"id\": 1,\n" +
"        \"empid\": \"12345\",\n" +
"        \"details\": {\n" +
"            \"name\": \"xyz\",\n" +
"            \"age\": \"30\",\n" +
"            \"sex\": \"M\",\n" +
"            \"Address\": {\n" +
"                \"Office\": \"office\",\n" +
"                \"Home\": \"Home\"\n" +
"            }\n" +
"        },\n" +
"        \"abcDetails\": \"asdf\",\n" +
"        \"mobile\": 123455\n" +
"    },\n" +
"    {\n" +
"        \"id\": 2,\n" +
"        \"empid\": \"64848\",\n" +
"        \"details\": {\n" +
"            \"name\": \"eryje\",\n" +
"            \"age\": 3027,\n" +
"            \"sex\": \"M\",\n" +
"            \"Address\": {\n" +
"                \"Office\": \"office\",\n" +
"                \"Home\": \"Home\"\n" +
"            }\n" +
"        },\n" +
"        \"abcDetails\": \"fhkdl\",\n" +
"        \"mobile\": 389928\n" +
"    }\n" +
"]}");


System.out.println((((Document)((ArrayList)root.get("records")).get(0)).get("id")));
System.out.println(((String)((Document)((ArrayList)root.get("records")).get(0)).get("empid")));
System.out.println(((String)((Document)((Document)((ArrayList)root.get("records")).get(0)).get("details")).get("name")));
System.out.println(((String)((Document)((Document)((ArrayList)root.get("records")).get(0)).get("details")).get("age")));
System.out.println(((String)((Document)((Document)((ArrayList)root.get("records")).get(0)).get("details")).get("sex")));
System.out.println(((String)((Document)((Document)((Document)((ArrayList)root.get("records")).get(0)).get("details")).get("Address")).get("Office")));
System.out.println(((String)((Document)((Document)((Document)((ArrayList)root.get("records")).get(0)).get("details")).get("Address")).get("Home")));
System.out.println(((String)((Document)((ArrayList)root.get("records")).get(0)).get("abcDetails")));
System.out.println((((Document)((ArrayList)root.get("records")).get(0)).get("mobile")));
System.out.println((((Document)((ArrayList)root.get("records")).get(1)).get("id")));
System.out.println(((String)((Document)((ArrayList)root.get("records")).get(1)).get("empid")));
System.out.println(((String)((Document)((Document)((ArrayList)root.get("records")).get(1)).get("details")).get("name")));
System.out.println((((Document)((Document)((ArrayList)root.get("records")).get(1)).get("details")).get("age")));
System.out.println(((String)((Document)((Document)((ArrayList)root.get("records")).get(1)).get("details")).get("sex")));
System.out.println(((String)((Document)((Document)((Document)((ArrayList)root.get("records")).get(1)).get("details")).get("Address")).get("Office")));
System.out.println(((String)((Document)((Document)((Document)((ArrayList)root.get("records")).get(1)).get("details")).get("Address")).get("Home")));
System.out.println(((String)((Document)((ArrayList)root.get("records")).get(1)).get("abcDetails")));
System.out.println((((Document)((ArrayList)root.get("records")).get(1)).get("mobile")));