java - JSONPath 获取 JSON 字符串形式的数组元素

标签 java json jsonpath jsonparser

我正在尝试用 Java 读取 JSON 数组的内容,并将每个元素作为 JSON 字符串获取。我的尝试失败了。

假设这是基本 JSON:

{ "book": [ 
      { "category": "",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": ""
      },
      { "category": "fiction",
        "author": "",
        "title": "Sword of Honour",
        "price": "12.99"
      },
      { "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": "8.99"
      },
      { "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ]
}

我遍历整个数组,并且需要将每个元素获取为 JSON 字符串(某些属性可以为空)。所以第一个字符串是:

{       "category": "",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": ""
}

我所做的是这样的:

String response = Unirest.get(db).header("cache-control", "no-cache").asString().getBody();

            int length = JsonPath.read(response, "$.book.length()");
            System.out.println(length);
            for (int i = 0; i < length; i++) {
                String json = JsonPath.read(response, "$.book["+i+"]").toString();

                System.out.println("1111111\n"+json);

                process(json);
            }

但是我得到的很乱,而且不是同一个字符串。它不包括""

解决办法是什么?

最佳答案

正如我在评论中提到的,JsonPath 将读取 JSON 对象并像这样返回它。它就像一个 HashMap,其类型由解析的内容定义。正如您所提到的,如果您希望在提取后将值作为 Json 形式,则需要将读回的内容再次转换为 Json:

public static void main(String[] args) {
    String response = "{ \"book\": [ " + 
            "      { \"category\": \"\"," + 
            "        \"author\": \"Nigel Rees\"," + 
            "        \"title\": \"Sayings of the Century\"," + 
            "        \"price\": \"\"" + 
            "      }," + 
            "      { \"category\": \"fiction\"," + 
            "        \"author\": \"\"," + 
            "        \"title\": \"Sword of Honour\"," + 
            "        \"price\": \"12.99\"" + 
            "      }," + 
            "      { \"category\": \"fiction\"," + 
            "        \"author\": \"Herman Melville\"," + 
            "        \"title\": \"Moby Dick\"," + 
            "        \"isbn\": \"0-553-21311-3\"," + 
            "        \"price\": \"8.99\"" + 
            "      }," + 
            "      { \"category\": \"fiction\"," + 
            "        \"author\": \"J. R. R. Tolkien\"," + 
            "        \"title\": \"The Lord of the Rings\"," + 
            "        \"isbn\": \"0-395-19395-8\"," + 
            "        \"price\": 22.99" + 
            "      }" + 
            "    ]" + 
            "}";
    int length = JsonPath.read(response, "$.book.length()");
    System.out.println(length);
    Configuration conf = Configuration.defaultConfiguration();

    Object document = Configuration.defaultConfiguration().jsonProvider().parse(response);

    for (int i = 0; i < length; i++) {
        String json = conf.jsonProvider().toJson(JsonPath.read(document, "$.book["+i+"]"));
        System.out.println(json);
        //process(json);
    }
}

这将输出:

{"category":"","author":"Nigel Rees","title":"Sayings of the Century","price":""}
{"category":"fiction","author":"","title":"Sword of Honour","price":"12.99"}
{"category":"fiction","author":"Herman Melville","title":"Moby Dick","isbn":"0-553-21311-3","price":"8.99"}
{"category":"fiction","author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}

我刚刚在 Docs 中了解到它,使用 Configuration 来解析 json 将使您的代码只解析一次。它甚至可以改进(删除 int length 的东西),但我会把它留给你:)

关于java - JSONPath 获取 JSON 字符串形式的数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54812676/

相关文章:

php - 从两个表创建自定义 json 数组

java - 当可以同时是 JSONArray 或 JSONObject 时,测试它是否是 JSONArray 或 JSONObject

amazon-redshift - 使用 JSONPath 进行 Redshift COPY 缺失的数组/字段

javascript - 用于 Firebase 规则的 JsonPath

jquery - 使用 jsonpath 进行字符串搜索

java - 使用类加载器隔离两个静态类

java - 如何使用 Firebase 向管理员显示所有登录用户数据?

java - 头尾递归的区别

java - 我可以将不同的 Canvas 笔划添加到数组中吗?

android - 数组反序列化之Gson Array