0

I am trying to parse through a JSON string and convert it to the following POJO:

package apicall;
//POJO representation of OAuthAccessToken
public class OAuthAccessToken {
    private String tokenType;
    private String tokenValue;
    public OAuthAccessToken(String tokenType,String tokenValue) {
        this.tokenType=tokenType;
        this.tokenValue=tokenValue;
    }

    public String toString() {
        return "tokenType="+tokenType+"\ntokenValue="+tokenValue;

    }

    public String getTokenValue() {
        return tokenValue;
    }

    public String getTokenType() {
        return tokenType;
    }

}

In order to do this I have written the following code:

Gson gson=new Gson();
String responseJSONString="{\"access_token\" : \"2YotnFZFEjr1zCsicMWpAA\",\"token_type\" : \"bearer\"}";
OAuthAccessToken token=gson.fromJson(responseJSONString, OAuthAccessToken.class);
System.out.println(token);

When I run the code, I get the following output:

tokenType=null
tokenValue=null

Instead of 
tokenType=bearer
tokenValue=2YotnFZFEjr1zCsicMWpAA

I don't understand if there's anything I've done wrong. Please help.

3 Answers 3

3

You can get the expected result by annotating your fields like:

@SerializedName("token_type")
private final String tokenType;
@SerializedName("access_token")
private final String tokenValue;
Sign up to request clarification or add additional context in comments.

Comments

1

How is Gson supposed to know how to populate your object? You don't have a no-arg constructor, and the fields of your object don't match the fields in the JSON object.

Make your object as following:

public class OAuthAccessToken {
    private String accessToken;
    private String tokenType;

    OAuthAccessToken() {
    }

    ...
}

2 Comments

This isn't the problem, in fact Gson doesn't require that you have a empty parameter constructor, when objects are restored no constructor is executed.
That's not what the documantations says: While deserializing an Object, Gson needs to create a default instance of the class Well-behaved classes that are meant for serialization and deserialization should have a no-argument constructor. Doesn't matter whether public or private. Typically, Instance Creators are needed when you are dealing with a library class that does NOT define a no-argument constructor. See sites.google.com/site/gson/… Anyway, the main point is that JSON and Java fields don't match at all.
0

The class should have the exact field name as the json, so if your json have 2 keys: "access_token" and "token_type", the class should have 2 fields:

private String access_token;
private String token_type;

And, of course you need to change the getters/setters accordingly.

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.