5

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:

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.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)

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...

How do I get the response into a JSONObject?

Kindle Q
9442 gold badges19 silver badges28 bronze badges
asked Oct 17, 2014 at 9:28

2 Answers 2

6

You can use this with Jackson 2.0 :

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

If your version is prior to 2.0 use :

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(
DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
answered Oct 17, 2014 at 10:27
Sign up to request clarification or add additional context in comments.

4 Comments

I've read about this, but how exactly do i use the readValue() Method?
Ok, nevermind i figured it out, but now i get a Exception in thread "main" org.codehaus.jackson.JsonParseException: Unexpected character ('h' (code 104)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
Sorry for so many answers..i'm working on it and got it running. Thanks for now!
@Maik care to share?
0

use annotation @JsonIgnoreProperties(ignoreUnknown = true) at class level import from packaage org.codehaus.jackson.annotate.JsonIgnoreProperties

answered Dec 5, 2024 at 6:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.