0

I have this result from server:
{"product_option_values":[{"id":43}, {"id":45}]}

And, I Have these classes to parse the above string for the class below

public class MyClass {

    @Key("product_option_values")
    private List<ProductOptionValueResult> values;

    public List<ProductOptionValueResult> getValues() {
        return values;
    }

    public void setValues(List<ProductOptionValueResult> values) {
        this.values = values;
    }
}

public class ProductOptionValueResult {

    private Integer id;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

I'm trying to convert the string above to an instance of MyClass like this:

MyClass myclass = gson.fromJson(stringAbove, MyClass.class);

However I get null in the object's values property, why?

2
  • I am not familiar with @Key - what library is it from? Commented Aug 25, 2020 at 1:05
  • Belongs to library GSON too. package com.google.api.client.util Commented Aug 25, 2020 at 12:35

1 Answer 1

1

Instead of this annotation:

@Key("product_option_values")

You can use this one:

@SerializedName("product_option_values")

This is from com.google.gson.annotations.SerializedName.

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

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.