【Java解析多层嵌套的JSON】

【Java解析多层嵌套的JSON】,第1张

Java解析多层嵌套的JSON 1. com.google.code.gson 1.1 引入依赖
<dependency>
    <groupId>com.google.code.gsongroupId>
    <artifactId>gsonartifactId>
    <version>2.8.0version>
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){

    //创建JSON解析器
    JsonParser parse = new JsonParser();

    //将String转成jsonObject
    JsonObject jsonObject = parse.parse(json).getAsJsonObject();

    //error
    int error = jsonObject.get("error").getAsInt();
    System.out.println("error: " + error);

    //status
    String status = jsonObject.get("status").getAsString();
    System.out.println("status: " + status);

    //results
    JsonArray results = jsonObject.get("results").getAsJsonArray();
    for (int i = 0; i < results.size(); i++){
        JsonObject jsonObject1 = results.get(i).getAsJsonObject();

        //currentCity
        String currentCity = jsonObject1.get("currentCity").getAsString();
        System.out.println("currentCity" + "-" + i + ": " + currentCity);

        //index
        JsonArray indexes = jsonObject1.get("index").getAsJsonArray();
        for (int j = 0; j < indexes.size(); j++){
            JsonObject jsonObject2 = indexes.get(j).getAsJsonObject();

            //title
            String title = jsonObject2.get("title").getAsString();
            System.out.println("title" + "-" + (j + 1) + ": " + title);

            //zs
            String zs = jsonObject2.get("zs").getAsString();
            System.out.println("zs" + "-" + (j + 1) + ": " + zs);

        }
    }
}
/*
error: 0
status: success
currentCity-0: bj
title-1: 穿衣
zs-1: 较冷
title-2: 紫外线强度
zs-2: 中等
*/
2. net.sf.json.JSONObject 2.1 引入依赖
  • maven依赖如下
<dependency>
    <groupId>net.sf.json-libgroupId>
    <artifactId>json-libartifactId>
    <version>2.4version>
    <classifier>jdk15classifier>// 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);

}
/*
[Stu(no=2020, name=张三), Stu(no=2021, name=李四), Stu(no=2022, name=王五)]      //list
[{"name":"张三","no":2020},{"name":"李四","no":2021},{"name":"王五","no":2022}]  //jSONArray
*/
2.3 测试代码
public static void jSONObjectTest(String json){

    JSONObject jsonObject = JSONObject.fromObject(json);

    //error
    int error = jsonObject.getInt("error");
    System.out.println("error: " + error);

    //status
    String status = jsonObject.getString("status");
    System.out.println("status: " + status);

    //results
    JSONArray results = jsonObject.getJSONArray("results");
    for (int i = 0; i < results.size(); i++) {

        //currentCity
        String currentCity = results.getJSONObject(i).getString("currentCity");
        System.out.println("currentCity: " + currentCity);
        //index
        JSONArray indexes = results.getJSONObject(i).getJSONArray("index");
        for (int j = 0; j < indexes.size(); j++) {

            //title
            String title = indexes.getJSONObject(j).getString("title");
            System.out.println("title" + "-" + (j + 1) + ": " + title);

            //zs
            String zs = indexes.getJSONObject(j).getString("zs");
            System.out.println("zs" + "-" + (j + 1) + ": " + zs);
        }
    }
}
/*
error: 0
status: success 
currentCity: bj
title-1: 穿衣
zs-1: 较冷
title-2: 紫外线强度
zs-2: 中等
*/
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";

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/866778.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-12
下一篇 2022-05-12

发表评论

登录后才能评论

评论列表(0条)

保存