Gson - 相同的字段名称,不同的类型

Asi*_*sim 24 android json gson

我今天在一个不同的问题中问过这个问题,但我担心因为它的措辞如何而无法得到任何解决方案.

我有一个json输入,其中包含以下数据:

JSON

如您所见,option_value项是一个对象中的Array和另一个对象中的简单字符串.

我怎样才能让Gson正确处理这个问题?我的类将其描述为List对象,因此它适用于其中option_value是数组的前几个项目,但是当它变为字符串时,应用程序崩溃并且我得到json解析异常.

这有解决方法吗?

UPDATE

按要求添加我班级的相关部分:

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
        }
    }
Run Code Online (Sandbox Code Playgroud)

Den*_*nko 24

我有一个解决方案:)为此我们应该使用自定义反序列化器.像这样重写你的课程:

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 ;
    }
}
}
Run Code Online (Sandbox Code Playgroud)

在我们让gson解析json之前,我们应该注册我们的自定义反序列化器:

Gson gson = new GsonBuilder()              
            .registerTypeAdapter(Options.class, new Options.OptionsDeserilizer())               
            .create();
Run Code Online (Sandbox Code Playgroud)

现在 - 只需致电:

Options options = gson.fromJson(json, Options.class);
Run Code Online (Sandbox Code Playgroud)

  • 我得到了一个StackOverflowError,因为在'Options options = new Gson().fromJson(json,Options.class);`和两个新的`Gson`实例在单个对象的反序列化中创建了自身的递归反序列化调用.小心. (2认同)

Sto*_*Lam 5

在我的情况下,同名字段是 "data":{} 或 "data":[ array_with_real_data ]。因此,接受答案中的代码需要稍作修改,如下所示:

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

    if (jsonObject.has("data")) {
        JsonArray array = jsonObject.getAsJsonArray("data");
        if (array != null && !array.isJsonNull()) {
            List<Data> data = new Gson().fromJson(array, new TypeToken<ArrayList<Data>>() {}.getType());
            bean.realData = data;
        }
    }
    return bean ;
}
Run Code Online (Sandbox Code Playgroud)

希望有帮助。