Java解析多层嵌套的JSON
1. com.google.code.gson
1.1 引入依赖
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
1.2 测试代码
- 创建JSON解析器 JsonParser parse = new JsonParser();
- 将String转成jsonObject JsonParser .parse(json).getAsJsonObject();
- JsonObject.get(“”).getAsInt();
- JsonObject.get(“”).getAsString();
- JsonObject.get(“”).getAsJsonArray();
public static void gsonTest(String json){
JsonParser parse = new JsonParser();
JsonObject jsonObject = parse.parse(json).getAsJsonObject();
int error = jsonObject.get("error").getAsInt();
System.out.println("error: " + error);
String status = jsonObject.get("status").getAsString();
System.out.println("status: " + status);
JsonArray results = jsonObject.get("results").getAsJsonArray();
for (int i = 0; i < results.size(); i++){
JsonObject jsonObject1 = results.get(i).getAsJsonObject();
String currentCity = jsonObject1.get("currentCity").getAsString();
System.out.println("currentCity" + "-" + i + ": " + currentCity);
JsonArray indexes = jsonObject1.get("index").getAsJsonArray();
for (int j = 0; j < indexes.size(); j++){
JsonObject jsonObject2 = indexes.get(j).getAsJsonObject();
String title = jsonObject2.get("title").getAsString();
System.out.println("title" + "-" + (j + 1) + ": " + title);
String zs = jsonObject2.get("zs").getAsString();
System.out.println("zs" + "-" + (j + 1) + ": " + zs);
}
}
}
2. net.sf.json.JSONObject
2.1 引入依赖
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>// jdk8也适用
</dependency>
2.2 JSONArray
List 转 JSONArray
public static void main(String[] args) {
ArrayList<Stu> list = new ArrayList<>();
list.add(new Stu(2020,"张三"));
list.add(new Stu(2021,"李四"));
list.add(new Stu(2022,"王五"));
JSONArray jSONArray = JSONArray.fromObject(list);
System.out.println(list);
System.out.println(jSONArray);
}
2.3 测试代码
public static void jSONObjectTest(String json){
JSONObject jsonObject = JSONObject.fromObject(json);
int error = jsonObject.getInt("error");
System.out.println("error: " + error);
String status = jsonObject.getString("status");
System.out.println("status: " + status);
JSONArray results = jsonObject.getJSONArray("results");
for (int i = 0; i < results.size(); i++) {
String currentCity = results.getJSONObject(i).getString("currentCity");
System.out.println("currentCity: " + currentCity);
JSONArray indexes = results.getJSONObject(i).getJSONArray("index");
for (int j = 0; j < indexes.size(); j++) {
String title = indexes.getJSONObject(j).getString("title");
System.out.println("title" + "-" + (j + 1) + ": " + title);
String zs = indexes.getJSONObject(j).getString("zs");
System.out.println("zs" + "-" + (j + 1) + ": " + zs);
}
}
}
3. 测试 json
String json = "{\n" +
" \"error\": 0,\n" +
" \"status\": \"success\",\n" +
" \"results\": [\n" +
" {\n" +
" \"currentCity\": \"bj\",\n" +
" \"index\": [\n" +
" {\n" +
" \"title\": \"穿衣\",\n" +
" \"zs\": \"较冷\",\n" +
" \"tipt\": \"穿衣指数\",\n" +
" \"des\": \"建议着厚外套加毛衣等服装。\"\n" +
" },\n" +
" {\n" +
" \"title\": \"紫外线强度\",\n" +
" \"zs\": \"中等\",\n" +
" \"tipt\": \"紫外线强度指数\",\n" +
" \"des\": \"属中等强度紫外线辐射天气。\"\n" +
" }\n" +
" ]\n" +
"\n" +
" }\n" +
" ]\n" +
"}\n";
|