JSON路径获取一个数组元素作为JSON字符串

JSONPath get an array element as JSON String

我正在尝试读取 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 对象并且 return 就像这样。它就像一个 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 中了解到它,使用配置来解析 json 将使您的代码只解析一次。它甚至可以改进(删除 int length 的东西),但我会留给你:)