0

We are using Google GSON for deserializing JSON data to Java object. See below sample JSON data and corresponding POJO class. I am able to deserialize "name" and "phoneNumber" fields. But for "deparments" field, I see Null. Can you please help me how can I deserialize "deparments" field?

My JSON:

{
  "name": "customer",
  "phoneNumber": "000000000",
  "deparments": "xyz,abc,wyz,djkf, iii"
}

The class:

public class CustomerInfo
{
    private String name;
    private String phoneNumber;
    private String deparments;

    // getters and setters
}

CustomerDeserializer.java:

...
Gson gson = new Gson();
CustomerInfo customerInfo = gson.fromJson(reader, CustomerInfo.class);
3
  • perhaps the Gson spellchecker dropped the property Commented Feb 19, 2016 at 14:42
  • Can you paste in the actual code? This clearly isn't it as there are spelling mistakes (e.g. "Privaet") and case inconsistencies, making it impossible to diagnose the actual problem. Commented Feb 19, 2016 at 14:58
  • 1
    Your code works for me, for GSON: 2.2.4 Commented Feb 19, 2016 at 15:39

2 Answers 2

1

Recheck your JSON source. I think you just have some objects with key: departments, instead of deparments.

Anyway next code works for me:

public class CustomerInfo
{
    private String name;
    private String phoneNumber;
    private String deparments;

    // getters and setters
    public String getName(){
        return name;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getPhoneNumber(){
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber){
        this.phoneNumber = phoneNumber;
    }
    public String getDeparments() {
        return deparments;
    }
    public void setDeparments(String deparments) {
        this.deparments = deparments;
    }

    @Override
    public String toString() {
        return "CustomerInfo{" +
                "name='" + name + '\'' +
                ", phoneNumber='" + phoneNumber + '\'' +
                ", deparments='" + deparments + '\'' +
                '}';
    }
}

@Test
public void desJson(){
    String json = "{\n" +
            "  \"name\": \"customer\",\n" +
            "  \"phoneNumber\": \"000000000\",\n" +
            "  \"deparments\": \"xyz,abc,wyz,djkf, iii\"\n" +
            "}";
    Gson gson = new Gson();
    CustomerInfo customerInfo = gson.fromJson(json, CustomerInfo.class);
    System.out.println(customerInfo);
}

the output is:

CustomerInfo{name='customer', phoneNumber='000000000', deparments='xyz,abc,wyz,djkf, iii'}

Sign up to request clarification or add additional context in comments.

Comments

0

Copy and Paste this code below

public class CustomerInfo {

    @SerializedName("name")
    private String name;
    @SerializedName("phoneNumber")
    private String phoneNumber;
    @SerializedName("deparments")
    private String deparments;

    @Override
    public String toString() {
        return new Gson().toJson(this, CustomerInfo.class);
    }
}

and Deserialize it like the code below

CustomerInfo customerInfo = new Gson().fromJson(jsonString, CustomerInfo.class);

Hope this helps you.

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.