下面是Web服务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。如何通过改型获得动态键json的响应?
答案 0 :(得分:0)
与翻新无关,而是gson。您正在使用gson(GsonConverterFactory
)进行解析,而不是进行翻新。 ,也许您应该为此模型注册TypeAdapter
并手动进行。
https://google.github.io/gson/apidocs/com/google/gson/TypeAdapter.html
答案 1 :(得分:0)
您可以在下面找到有关动态密钥json提取响应的另一个答案
JSON
{
"tom@example.com":{
"_from":"giles@gmail.com",
"message":{
"from":"giles@gmail.com",
"detail":"AnyonewanttomeetmeinParis"
}
},
"harry@example.com":{
"_from":"giles@gmail.com",
"message":{
"from":"giles@gmail.com",
"detail":"AnyonewanttomeetmeinParis"
}
}
}
网络服务呼叫
ApiHandler.getApiService().place_order2(
new Callback<Object>() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void success(Object placeOrderModel, Response
response) {
Log.e("WWWWWWWWWWWWWWWWWWW",""+placeOrderModel.toString());
JSONObject jsonData = null;
try {
jsonData = new JSONObject(placeOrderModel.toString());
} catch (JSONException e) {
e.printStackTrace();
}
Iterator itr = jsonData.keys();
while(itr.hasNext()) {
String keys = (String) itr.next();
Log.e("Keys", "----" + keys);
try {
JSONObject issue = jsonData.getJSONObject(keys);
String _pubKey = issue.optString("_from");
Log.e("_from", "----" + _pubKey);
dataModel = new DataModel(keys,_pubKey);
dataModelArrayList.add(dataModel);
} catch (JSONException e) {
e.printStackTrace();
}
}
historyAdapter = new HistoryAdapter(dataModelArrayList);
rvHistoryList.setAdapter(historyAdapter);
}
@Override
public void failure(RetrofitError error) {
}
});
答案 2 :(得分:-1)
我有解决方案,请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;
}
}
}
网络服务呼叫
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) {
}
});