Json parsing for Java object

got this problem: Json:

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

An 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 aa error in the logs: could not read JSON: unrecognized field "authenticationToken" (class de.regalfrei.android.AuthenticationToken), not marked as ignorant (one well-known property: "token"]), and I do not understand the idea how to properly set the JSON properties for this situation. Can anyone 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);
+4
source share
3 answers

You are using a wrapper class that has a variable with a name authenticationTokenthat is an objectauthenticationToken

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;
    }
}
+6

...

, private String authenticationToken; , authenticToken - , , JSON, JSON. JSON .

+1

to try

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

then un-marshall this line to another class with

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

Source: https://habr.com/ru/post/1531289/


All Articles