java - 使用 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", "{}"]
]
}
解决方案
使用一个名为 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());
}
}
推荐阅读
- mongodb - 无法将机器 1 上的 Logstash 的数据发送到机器 2 上运行的 MongoDB
- node.js - NODE JS API ENCODE 转为字符串
- java - extent report 5 合并不同机器的不同报告结果
- flutter - 如何更正对齐并删除不需要的空间
- visual-studio-2019 - 包管理器控制台没有输出
- flutter - 应用程序崩溃时如何显示自定义对话框
- c# - 如何将 GeoJSON 数据绑定到 ASP.NET Core 中的参数?
- ios - 有没有办法在 UIAlertAction 中获取文本字段值?
- java - 如何在 kotlin 中对列表进行分组和合并?
- amazon-web-services - AWS 中的 Terraform 技术用户需要哪些权限?