无法读取 JSON:无法识别的字段 (...),未标记为可忽略
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26421975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not read JSON: Unrecognized field (...), not marked as ignorable
提问by Maik
Yeah, I know that this issue has been discussed a few times, but I didn't manage to solve my problem.
是的,我知道这个问题已经讨论过几次了,但是我没有设法解决我的问题。
So I'm getting a JSONObject from a http-request using org.springframework.web.client.RestTemplate:
因此,我使用org.springframework.web.client.RestTemplate从 http 请求中获取 JSONObject :
JSONObject j = RestTemplate.getForObject(url, JSONObject.class);
But I get this error:
但我收到此错误:
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.Hymanson.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.MappingHymansonHttpMessageConverter.readJavaType(MappingHymansonHttpMessageConverter.java:181)
at org.springframework.http.converter.json.MappingHymansonHttpMessageConverter.read(MappingHymansonHttpMessageConverter.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)
I want to access a Rest-Api and the Json Objects can have different field names.
I already tried @JsonIgnoreProperties(ignoreUnknown=true). But this won't work...
我想访问一个 Rest-Api 并且 Json 对象可以有不同的字段名称。我已经试过了@JsonIgnoreProperties(ignoreUnknown=true)。但这行不通...
How do I get the response into a JSONObject?
如何将响应放入 JSONObject?
回答by alain.janinm
You can use this with Hymanson 2.0 :
您可以在 Hymanson 2.0 中使用它:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
If your version is prior to 2.0 use :
如果您的版本在 2.0 之前,请使用:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(
DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
回答by Jeet
org.codehaus.Hymanson.JsonParseException: Unexpected character ('h' (code 104)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
org.codehaus.Hymanson.JsonParseException: Unexpected character ('h' (code 104)): 应为有效值(数字、字符串、数组、对象、“真”、“假”或“空”)
**Note:**You are getting error because you are not sending data in right JSON format.
**注意:**您收到错误,因为您没有以正确的 JSON 格式发送数据。

