gpt4 book ai didi

json - 无法读取 JSON : Unrecognized field (. ..),未标记为可忽略

转载 作者:行者123 更新时间:2023-12-01 11:36:37 29 4
gpt4 key购买 nike

是的,我知道这个问题已经讨论过几次了,但是我没有设法解决我的问题。

所以我使用 从 http 请求中获取 JSONObject org.springframework.web.client.RestTemplate:

JSONObject j = RestTemplate.getForObject(url, JSONObject.class);

但我收到此错误:
    Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unrecognized field "uri" (Class org.json.JSONObject), not marked as ignorable
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@6f526c5f; line: 2, column: 12] (through reference chain: org.json.JSONObject["uri"]); nested exception is org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "uri" (Class org.json.JSONObject), not marked as ignorable
at [Source: sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@6f526c5f; line: 2, column: 12] (through reference chain: org.json.JSONObject["uri"])
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readJavaType(MappingJacksonHttpMessageConverter.java:181)
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.read(MappingJacksonHttpMessageConverter.java:173)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:94)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:517)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:472)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:237)

我想访问一个 Rest-Api 并且 Json 对象可以有不同的字段名称。
我已经试过了 @JsonIgnoreProperties(ignoreUnknown=true) .但这行不通...

如何将响应放入 JSONObject?

最佳答案

您可以在 Jackson 2.0 中使用它:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

如果您的版本在 2.0 之前,请使用:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(
DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

关于json - 无法读取 JSON : Unrecognized field (. ..),未标记为可忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26421975/

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