Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

android - Gson - Same field name, different types

I asked this in a different question today but I'm afraid that won't get any solution because of how it was phrased.

I have a json input that has the following data:

Json

As you can see, the option_value item is an Array in one object and a simple string in another object.

How can I make Gson handle this properly? My class has this described as a List object, so it works for the first few items where option_value is an array, but when it becomes a string, the app crashes and I get a json parse exception.

Is there a workaround for this?

UPDATE

Adding the relevant part of my class as requested:

public class Options
    {
        String product_option_id;
        String option_id;
        String name;
        String type;
        String required;
        List<OptionValue> option_value;

        // get set stuff here

        public class OptionValue
        {
            String product_option_value_id;
            String option_value_id;
            String name;
            String image;
            String price;
            String price_prefix;

            // get set stuff here
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have a solution for you :) For this purpose we should use custom deserializer. Remake your class like this:

public class Options{

    @SerializedName ("product_option_id");
    String mProductOptionId;

    @SerializedName ("option_id");
    String mOptionId;

    @SerializedName ("name");
    String mName;

    @SerializedName ("type");
    String mType;

    @SerializedName ("required");
    String mRequired;

    //don't assign any serialized name, this field will be parsed manually
    List<OptionValue> mOptionValue;

    //setter
    public void setOptionValues(List<OptionValue> optionValues){
         mOptionValue = optionValues;
    }

    // get set stuff here
    public class OptionValue
    {
        String product_option_value_id;
        String option_value_id;
        String name;
        String image;
        String price;
        String price_prefix;

        // get set stuff here
    }

public static class OptionsDeserilizer implements JsonDeserializer<Options> {

    @Override
    public Offer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        Options options = new Gson().fromJson(json, Options.class);
        JsonObject jsonObject = json.getAsJsonObject();

        if (jsonObject.has("option_value")) {
            JsonElement elem = jsonObject.get("option_value");
            if (elem != null && !elem.isJsonNull()) {  
                 String valuesString = elem.getAsString();
                 if (!TextUtils.isEmpty(valuesString)){
                     List<OptionValue> values = new Gson().fromJson(valuesString, new TypeToken<ArrayList<OptionValue>>() {}.getType());
                     options.setOptionValues(values);
                 }
            }
        }
        return options ;
    }
}
}

Before we can let gson parse json, we should register our custom deserializer:

Gson gson = new GsonBuilder()              
            .registerTypeAdapter(Options.class, new Options.OptionsDeserilizer())               
            .create();

And now - just call:

Options options = gson.fromJson(json, Options.class);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...