【问题标题】:JSONPath get an array element as JSON StringJSONPath 以 JSON 字符串形式获取数组元素
【发布时间】:2019-02-21 17:12:42
【问题描述】:

我正在尝试读取 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,所以,请首先向我们展示您使用的是哪个库(JsonPath?)以及您是什么结果(你所说的混乱是什么)。这将有助于我们识别您的问题并为您提供更好的帮助。仅供参考,我以前从未使用过这个 JsonPath...
  • JSONPath 很直观,所以到目前为止我一直在使用它。好吧,@MiguelCruz 建议的那个有效,所以不用再费心使用 JSONPath 了!
  • 我刚刚在这里测试了您的代码,它确实按“预期”工作。您正在使用一个读取 json 的库,因此它将响应一个类似 json 的对象,因此不需要值中的双引号。例如,如果您添加代码String json = JsonPath.read(response, "$.book["+i+"].category").toString(),它将为第一个打印空白,为所有其他打印虚构...如果您希望它实际将其打印为新的 json 对象,您需要为您的阅读提供特定的配置方法。刚刚通过阅读文档了解到它:)

标签: java json jsonpath jsonparser


【解决方案1】:

正如我在 cmets 中提到的,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了解到,使用配置解析json会让你的代码只解析一次。它甚至可以改进(删除int length 的东西),但我会留给你:)

【讨论】:

  • 有趣!是的,很好的解决方案。虽然我已经使用了org.json 库。
  • 没问题。这样做的好处是我可以学到一些新的东西 :)
  • 顺便说一句,我真的很喜欢 JsonPath 处理它的方式,并提供了执行谓词、分组和其他东西的方法,就像 java 代码中的 Mongo... 我会对其进行一些性能测试如果它足够好,从现在开始使用它。
  • 是的,它非常适合在 JSON 中查找特定值。我想知道他们为什么不考虑 $.book[i] 返回项目的 JSON!
  • 我最好的选择是,这是由于性能原因……为了理解 json,您需要将其解析为某种东西(在他们的情况下是映射结构),因此将其转换为其他东西。 .. 为了返回 json 的原始部分,您必须将其转换回来,因此将 toJson 方法添加到 JsonPath 类本身可能会更容易,因此可以从 read 方法中调用,如...read(...).toJson()
猜你喜欢
  • 1970-01-01
  • 2020-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-29
  • 2014-09-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多