我为您提供解决方案:)为此,我们应该使用自定义
解串器。重新制作您的课程,如下所示:
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);



