使用Java解析JSON响应

时间:2019-03-04 06:03:24

标签: java json

如何使用Java解析此json响应

{
    "Name": {
        "name_description": "NIL",
        "date": "NIL"
    },
    "Age": {},
    "City": {},
    "SOAP": [
        ["content", "subtopic", "topic", "code"],
        ["I advised her to call 911, which he did.", "history of present illness", "subjective", "{}"]
    ]
}

2 个答案:

答案 0 :(得分:1)

您必须使用json-simple之类的外部库

详细了解here

答案 1 :(得分:1)

使用一个名为org.json的库,这实际上是最好的java json库。

例如:

import org.json.JSONObject;

private static void createJSON(boolean prettyPrint) {    
   JSONObject tomJsonObj = new JSONObject();
   tomJsonObj.put("name", "Tom");
   tomJsonObj.put("birthday", "1940-02-10");
   tomJsonObj.put("age", 76);
   tomJsonObj.put("married", false);

// Cannot set null directly
tomJsonObj.put("car", JSONObject.NULL);

tomJsonObj.put("favorite_foods", new String[] { "cookie", "fish", "chips" });

// {"id": 100001, "nationality", "American"}
JSONObject passportJsonObj = new JSONObject();
passportJsonObj.put("id", 100001);
passportJsonObj.put("nationality", "American");
// Value of a key is a JSONObject
tomJsonObj.put("passport", passportJsonObj);

if (prettyPrint) {
    // With four indent spaces
    System.out.println(tomJsonObj.toString(4));
} else {
    System.out.println(tomJsonObj.toString());
}

}