gpt4 book ai didi

java - 与 Jackson 一起使用 Jayway JsonPath

转载 作者:太空宇宙 更新时间:2023-11-04 09:50:56 36 4
gpt4 key购买 nike

我一直在使用JsonPath。然而,昨天出现一个问题,我发现默认的 JsonSmartJsonProvider 在解析时没有报告无效文档的错误,我修改了我的设置以使用 Jackson,如下所示

public JsonPathExtractor(String document) throws DocumentFormatException
{
try
{
Configuration.setDefaults(new Configuration.Defaults()
{
private final JsonProvider jsonProvider = new JacksonJsonProvider();
private final MappingProvider mappingProvider = new JacksonMappingProvider();

@Override
public JsonProvider jsonProvider()
{
return jsonProvider;
}

@Override
public MappingProvider mappingProvider()
{
return mappingProvider;
}

@Override
public Set<Option> options()
{
return EnumSet.noneOf(Option.class);
}
});

// Get an object representation of the JSON to allow values to be extracted
this.document = Configuration.defaultConfiguration().jsonProvider().parse(document);
}
catch(Exception e)
{
throw new DocumentFormatException("Invalid JSON document", e);
}
}

但是,我发现行为有所不同,因为如果我获得包含几个字段的路径,则它们不会被引用,而使用 JsonSmartJsonProvider 时会被引用。
JSON 示例

{
"firstName": "John",
"lastName": "Smith",
"isAlive": true,
"age": 25,
"height_cm": 167.6,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021-3100"
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "office",
"number": "646 555-4567"
}
],
"children": [],
"spouse": null
}

通过电话:

Object obj = JsonPath.read(document, "$.phoneNumbers"); 

当使用JacksonMappingProvider时,我得到

[{type=home, number=212 555-1234}, {type=office, number=646 555-4567}] 

当使用JsonSmartJsonProvider时,我得到:

[{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}]  

如果我希望 Jackson 的行为方式相同,我还可以配置其他内容吗?

最佳答案

jackson 处理这些值的方式和打印这些值的方式之间存在差异。

当使用JsonSmartJsonProvider这一行时...

JsonPath.read(parse, "$.phoneNumbers");

... 返回一个 JSONArraytoString() 方法 - 当您“打印”JSONArray 实例时调用该方法,它足够聪明,知道它正在处理 JSON,因此它将该状态打印为 JSON 字符串。例如:

[{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}]

但是当您使用 JacksonJsonProvider 时,这一行...

JsonPath.read(parse, "$.phoneNumbers");

...返回 LinkedHashMapList 以及当您“打印”该实例时调用的 toString() 实现,该实例不支持 JSON,因此它会打印以下内容:

[{type=home, number=212 555-1234}, {type=office, number=646 555-4567}]

如果您想在使用 JacksonJsonProvider 时打印 JSON,那么您必须使用支持 JSON 的内容来打印它。这是一个例子:

String payload = "{\n" +
" \"firstName\": \"John\",\n" +
" \"lastName\": \"Smith\",\n" +
" \"isAlive\": true,\n" +
" \"age\": 25,\n" +
" \"height_cm\": 167.6,\n" +
" \"address\": {\n" +
" \"streetAddress\": \"21 2nd Street\",\n" +
" \"city\": \"New York\",\n" +
" \"state\": \"NY\",\n" +
" \"postalCode\": \"10021-3100\"\n" +
" },\n" +
" \"phoneNumbers\": [\n" +
" {\n" +
" \"type\": \"home\",\n" +
" \"number\": \"212 555-1234\"\n" +
" },\n" +
" {\n" +
" \"type\": \"office\",\n" +
" \"number\": \"646 555-4567\"\n" +
" }\n" +
" ],\n" +
" \"children\": [],\n" +
" \"spouse\": null\n" +
"}";

// this is a simpler way of declaring and using the JacksonJsonProvider
ObjectMapper objectMapper = new ObjectMapper();

Configuration conf = Configuration.builder()
.jsonProvider(new JacksonJsonProvider(objectMapper))
.build();

Object obj = JsonPath.using(conf).parse(payload).read("$.phoneNumbers");

// prints out:
// [{type=home, number=212 555-1234}, {type=office, number=646 555-4567}]
System.out.println(obj);

// prints out:
// [{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}]
System.out.println(objectMapper.writer().writeValueAsString(obj));

关于java - 与 Jackson 一起使用 Jayway JsonPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54762090/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com