gpt4 book ai didi

java - 如何将此 JSON 响应解析为 POJO

转载 作者:行者123 更新时间:2023-12-02 11:00:27 31 4
gpt4 key购买 nike

我有以下测试类:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ibm.cio.cloud.cost.model.ElasticResponse;
import com.jayway.jsonpath.JsonPath;

@JsonIgnoreProperties(ignoreUnknown = true)
public class TestJSONPaths {

private static final String json = "{\"hits\":{\"total\":1,\"hits\":[{\"_id\":\"oEE4j2QBXCNPxFWHqq3i\",\"_score\":1.0,\"_source\":{\"status\":\"SUCCESSFUL\",\"reason\":\"OK, Single ACTIVE status can process\"}}]}}";

public static void main(String[] args) {
List<String> strippedJSON = JsonPath.read(json, "$.hits.hits._source");
ElasticResponse response = null;
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);

try {
System.out.println("From this json string:" + strippedJSON + "\n");
response = mapper.readValue(strippedJSON.toString(), ElasticResponse.class);
System.out.println("ElasticDocument=" + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(response.getDocuments()));
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

这是 ElasticResponse 类定义:

public class ElasticResponse {
private List<ElasticDocument> documents;
public List<ElasticDocument> getDocuments() {
return documents;
}
public void setDocuments(List<ElasticDocument> documents) {
this.documents = documents;
}
}

public class ElasticDocument {
private String _id;

private String status;
private String reason;

... getters/setters
}

我试图获取从给定的 JSON 映射的 ElasticDocument 对象,但它抛出以下错误。我试图将 JSON 过滤为:[{_source document value }]。此错误发生在 Main 类的第一行。我怎样才能映射这个Json?

[DEBUG] Evaluating path: $['hits']['hits']['_source']
Exception in thread "main" com.jayway.jsonpath.PathNotFoundException: Expected to find an object with property ['_source'] in path $['hits']['hits'] but found 'net.minidev.json.JSONArray'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.
at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:71)
at com.jayway.jsonpath.internal.path.PathToken.handleObjectProperty(PathToken.java:81)
at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:79)
at com.jayway.jsonpath.internal.path.PathToken.handleObjectProperty(PathToken.java:81)
at com.jayway.jsonpath.internal.path.PropertyPathToken.evaluate(PropertyPathToken.java:79)
at com.jayway.jsonpath.internal.path.RootPathToken.evaluate(RootPathToken.java:62)
at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:53)
at com.jayway.jsonpath.internal.path.CompiledPath.evaluate(CompiledPath.java:61)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:187)
at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:102)
at com.jayway.jsonpath.internal.JsonContext.read(JsonContext.java:89)
at com.jayway.jsonpath.JsonPath.read(JsonPath.java:502)
at com.ibm.cio.cloud.cost.TestJSONPaths.main(TestJSONPaths.java:18)

最佳答案

异常是由于 jsonpath 返回数组而不是对象,因此如果将 jsonpath 修复为如下所示:

$.hits.hits[*]._source

然后它将正确评估。然而,这可能仍然没有达到您想要的效果。JsonPath.read() 将为您反序列化 JSON。但你必须注意这一点:

public class Test {

private static final String json = "{\"hits\":{\"total\":1,\"hits\":[{\"_id\":\"oEE4j2QBXCNPxFWHqq3i\",\"_score\":1.0,\"_source\":{\"status\":\"SUCCESSFUL\",\"reason\":\"OK, Single ACTIVE status can process\"}}]}}";


public static void main(String[] args) {
List<ElasticDocument> docs = JsonPath.read(json, "$.hits.hits[*]._source");

System.out.println("elasticDoc: " + docs.get(0));
}

public static class ElasticDocument {
public String _id;

public String status;
public String reason;

@Override
public String toString() {
return "ElasticDocument{" +
"_id='" + _id + '\'' +
", status='" + status + '\'' +
", reason='" + reason + '\'' +
'}';
}
}
}

看起来它可以工作,但是 docs List 现在实际上是 MapList。显然可以向 Jackson 注册 JsonPath,但我无法使其工作

或者,您可以使用 Jackson 反序列化 JSON,然后您应该创建一个与 json 结构匹配的对象模型,然后您可以使用 ObjectMapper 进行反序列化

public class Test {

private static final String json = "{\"hits\":{\"total\":1,\"hits\":[{\"_id\":\"oEE4j2QBXCNPxFWHqq3i\",\"_score\":1.0,\"_source\":{\"status\":\"SUCCESSFUL\",\"reason\":\"OK, Single ACTIVE status can process\"}}]}}";

public static void main(String[] args) {

System.out.println("From this json string:" + json + "\n");

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);

try {

HitsResource hitsResource = mapper.readValue(json, HitsResource.class);

System.out.println("jackson elasticDoc: " + hitsResource.hitsParent.hits.get(0).source);

} catch (Exception e) {
e.printStackTrace();
}
}

public static class HitsResource {
@JsonProperty("hits")
public HitsParent hitsParent;

@Override
public String toString() {
return "HitsResource{" +
"hitsParent=" + hitsParent +
'}';
}
}

public static class HitsParent {
@JsonProperty("total")
public Long total;
@JsonProperty("hits")
public List<Hits> hits;

@Override
public String toString() {
return "HitsParent{" +
"total=" + total +
", hits=" + hits +
'}';
}
}

public static class Hits {
@JsonProperty("_id")
public String id;
@JsonProperty("_score")
public BigDecimal score;
@JsonProperty("_source")
public ElasticDocument source;

@Override
public String toString() {
return "Hits{" +
"id='" + id + '\'' +
", score=" + score +
", source=" + source +
'}';
}
}

public static class ElasticDocument {
@JsonProperty("_id")
public String _id;

@JsonProperty("status")
public String status;

@JsonProperty("reason")
public String reason;

@Override
public String toString() {
return "ElasticDocument{" +
"_id='" + _id + '\'' +
", status='" + status + '\'' +
", reason='" + reason + '\'' +
'}';
}
}
}

关于java - 如何将此 JSON 响应解析为 POJO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51371785/

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