动态密钥json从WebService获取响应?

android web-services android-studio retrofit retrofit2

30 观看

2回复

2021 作者的声誉

以下是webservice URL响应:

{  
 "Status":"Success",
 "StatusCode":"200",
 "Message":"data fetch successfully.",
 "Data":{  
  "1":{  
     "OptionGroupName":"OPTION",
     "OptionGroupId":"122",
     "OptionCount":2,
     "Options":[  
        {  
           "OptionGroupId":"122",
           "OptionGroupName":"Option",
           "ProductId":"2861",
           "OptionId":"329",
           "OptionName":"Hot",
           "OptionPrice":"4.00",
           "IsActive":"1"
        },
        {  
           "OptionGroupId":"122",
           "OptionGroupName":"Option",
           "ProductId":"2861",
           "OptionId":"330",
           "OptionName":"Ice",
           "OptionPrice":"4.30",
           "IsActive":"1"
        }
     ]
  }
 }
}

下面是模型类;

public class Elements{

@SerializedName("Data")
@Expose
private Map<String, Data> Data;

public Map<String, Data> getFeeds() {
    return Data;
}

public void setFeeds(Map<String, Data> Data) {
    this.Data = Data;
}

public class Data {
    @SerializedName("OptionGroupName")
    @Expose
    private String OptionGroupName;

    public String getOptionGroupName() {
        return OptionGroupName;
    }

    public void setOptionGroupName(String optionGroupName) {
        OptionGroupName = optionGroupName;
    }
}

}

以下是获取响应的代码;

  Map<String, String> jsonObject = new HashMap<>();
    jsonObject.put("productid", "2861");
    jsonObject.put("multiple", "1");

    ApiHandler.getApiService().place_order(jsonObject,
            new Callback<Elements>() {
                @RequiresApi(api = Build.VERSION_CODES.KITKAT)
                @Override
                public void success(Elements placeOrderModel, Response
                        response) {

                    Log.e("response.getElemDetails()",""+response.getBody());
                    Gson gson = new Gson();
                    Elements myObj  = gson.fromJson(response.toString(),
                            new TypeToken<Elements>(){}.getType());


                    for (int i=0;i<myObj.getFeeds().size();i++)
                    {
                        Log.e("data",""+myObj.getFeeds().get(i).getOptionGroupName());
                    }
                }

                @Override
                public void failure(RetrofitError error) {

                }
            });

我不能用模型类解析json ..plz帮助如何使用retrofit获得动态密钥json的响应?

作者: Vishal Vaishnav 的来源 发布者: 2019 年 3 月 26 日

回应 2


0

1747 作者的声誉

它与改造无关,但是gson。你正在用gson(GsonConverterFactory)解析,而不是改造。,可能你应该注册TypeAdapter这个模型并手动完成。

https://google.github.io/gson/apidocs/com/google/gson/TypeAdapter.html

作者: RadekJ 发布者: 2019 年 3 月 26 日

0

2021 作者的声誉

我得到了解决方案,Plz回顾我的答案;

Elements.java

public class Elements{

@SerializedName("Data")
@Expose
private Map<String, Data> Data;

public Map<String, Data> getFeeds() {
    return Data;
}

public void setFeeds(Map<String, Data> Data) {
    this.Data = Data;
}

public class Data {
    @SerializedName("OptionGroupName")
    @Expose
    private String OptionGroupName;

    public String getOptionGroupName() {
        return OptionGroupName;
    }

    public void setOptionGroupName(String optionGroupName) {
        OptionGroupName = optionGroupName;
    }

    @Override
    public String toString() {
        return "Data{" +
                "OptionGroupName='" + OptionGroupName + '\'' +
                '}';
    }

    @SerializedName("Options")
    @Expose
    ArrayList<OptionsList> Options;

    public ArrayList<OptionsList> getOptions() {
        return Options;
    }

    public void setOptions(ArrayList<OptionsList> options) {
        Options = options;
    }
}

@Override
public String toString() {
    return "Elements{" +
            "Data=" + Data +
            '}';
}

public class OptionsList {
    @SerializedName("OptionName")
    @Expose
    private String OptionName;

    public String getOptionGroupName() {
        return OptionName;
    }

    public void setOptionGroupName(String OptionName) {
        OptionName = OptionName;
    }
}
}

Web服务调用

 Map<String, String> jsonObject = new HashMap<>();
    jsonObject.put("productid", "2855");
    jsonObject.put("multiple", "1");

    ApiHandler.getApiService().place_order(jsonObject,
            new Callback<Elements>() {
                @RequiresApi(api = Build.VERSION_CODES.KITKAT)
                @Override
                public void success(Elements placeOrderModel, Response
                        response) {

                    Map<String, Elements.Data> entityList = placeOrderModel.getFeeds();

                    for (String mapKey : entityList.keySet()) {
                        Log.e("Map", "mapKey : " + mapKey + " , mapValue : " + entityList.get(mapKey).getOptionGroupName());

                        for (int i = 0; i < entityList.get(mapKey).getOptions().size(); i++) {
                            Log.e("!!!!!!!!!!!!!", "" + entityList.get(mapKey).getOptions().get(i).getOptionGroupName());
                        }
                    }

                }

                @Override
                public void failure(RetrofitError error) {

                }
            });
作者: Vishal Vaishnav 发布者: 2019 年 3 月 26 日
32x32