4

got this problem: Json:

{"authenticationToken":{"token":"c9XXXX1-XXXX-4XX9-XXXX-41XXXXX3XXXX"}}

Object:

    public class AuthenticationToken {
 public AuthenticationToken() {

 }

 public AuthenticationToken(String token) {
  authenticationToken = token;
 }

    @JsonProperty(value="token")
    private String authenticationToken;


 public String getAuthenticationToken() {
  return authenticationToken;
 }

 public void setAuthenticationToken(String authenticationToken) {
  this.authenticationToken = authenticationToken;
 }
}

But i got a a error in logs: Could not read JSON: Unrecognized field "authenticationToken" (class de.regalfrei.android.AuthenticationToken), not marked as ignorable (one known property: "token"]) and i do not have any idea how to set the JSON properties correct for this situation. Can someone help?

As you said i added a Wrapperclass:

public class AuthenticationTokenWrapper {
    AuthenticationToken authenticationToken;

    public AuthenticationTokenWrapper(AuthenticationToken authenticationToken) {
        this.authenticationToken = authenticationToken;
    }
    @JsonProperty(value="authenticationToken")
    public AuthenticationToken getAuthenticationToken() {
        return authenticationToken;
    }

    public void setAuthenticationToken(AuthenticationToken authenticationToken) {
        this.authenticationToken = authenticationToken;
    }

}

and called this function:

AuthenticationTokenWrapper tok =restTemplate.postForObject(url, requestEntity, AuthenticationTokenWrapper.class);
10
  • You probably have to write a custom deserializer here... Commented Mar 12, 2014 at 8:17
  • Hi, what dies this mean? custom deserializer? Commented Mar 12, 2014 at 8:54
  • 1
    That or use the @JsonUnwrap annotation. Commented Mar 12, 2014 at 9:04
  • which deserializer are you using? Commented Mar 12, 2014 at 9:06
  • I tried this: @JsonUnwrapped public AuthenticationToken getAuthenticationToken() { but also got this error Could not read JSON: No _valueDeserializer assigned at Commented Mar 12, 2014 at 9:11

3 Answers 3

6

You are using a wrapper class which have a variable named authenticationToken which is an object of AuthenticationToken

in order to parse your JSON correctly, create a wrapper class like this

public class Wrapper {
private AuthenticationToken authenticationToken;

public Wrapper(AuthenticationToken authenticationToken) {
    this.authenticationToken = authenticationToken;
}

public AuthenticationToken getAuthenticationToken() {
    return authenticationToken;
}

public void setAuthenticationToken(AuthenticationToken authenticationToken) {
    this.authenticationToken = authenticationToken;
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, thanks. i got this: No suitable constructor found for type [simple type, class de.myapp.android.AuthenticationTokenWrapper]: can not instantiate from JSON object (need to add/enable type information?)
Is the @JsonProperty(value="authenticationToken") in the wrapper class correct? i also tried instead this: @JsonUnwrapped but also does not work..
I am using Jackson deserializer which does not require explicit annotation when the getter and setter methods are with the same name as the variables. Other deserializers may need explicit annotations.
I don't know which deserializer would Spring use, I think this will help.
I have solved it.. restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter()); System.out.println(restTemplate.postForObject(url, requestEntity, String.class)); AuthenticationTokenWrapper tok =restTemplate.postForObject(url, requestEntity, AuthenticationTokenWrapper.class) ; i used MappingJackson2HttpMessageConverter instead of MappingJacksonHttpMessageConverter. but now it worked with the wrapper class as you suggested!
|
1

I am not sure about this...

Maybe the error is here private String authenticationToken; You are saying authenticationToken is a string, but according to the JSON Object it is another JSON Object. Try converting it into JSON Object and access the token.

3 Comments

Hi, you mean the same thing like Manu Viswam correct? this i tried and does not work :(
@ludwigmi I don't understand what he says... I am saying that you can access it as JSONObject a=new JSONObject('{"authenticationToken":{"token":"c9XXXX1-XXXX-4XX9-XXXX-41XXXXX3XXXX"}}'); you can access token as Sysout(((JSONObject)a.get("authenticationToken")).get("token").toString()); Note: I am using org.json.jar It works for me.
Hi, yes, this i think will work. But i want to load this JASON into my Java Object Class AuthenticationToken.
0

try

@JsonProperty(value="authenticationToken")
private String authenticationToken;

then un-marshall that string into another class with

@JsonProperty(value="token")
private String tokenStr;

Comments

Your Answer

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.