第一种情况
PS:下面的dates里面的字段都是后台返回的,里面的字段都不是固定的
{
"productcode": "000001",
"productname": "平安银行",
"dates": {
"q": "cwffdcc",
"w": "www",
"g": "gfdfdg"
}
}
当JSON数据是这样时候解析如下
Gson gson = new Gson();
Tests p = gson.fromJson(str, Tests.class);
for (String key : p.getDates().keySet()) {
Log.e("syp", key + "---" + p.getDates().get(key));
}
public class Tests {
private String productcode;
private String productname;
private Map<String, String> dates;
//set get....
第二种情况
{
"productcode": "000001",
"productname": "平安银行",
"dates": {
"a1": "aaaaaaa",
"a2": "bbbbb",
"a3": "ccccc",
"test":{
"x1":"xxx",
"x2":"yyy",
"x3":"zzz"
}
}
}
当JSON数据是这样时候解析如下
Gson gson = new Gson();
Tests p = gson.fromJson(str, Tests.class);
//遍历
for (String key : p.getDates().keySet()) {
Log.e("syp", key + "---" + p.getDates().get(key));
try {
//取值后转换成json格式数据
String jsonstr = gson.toJson(p.getDates().get(key));
//将数据转成map集合,针对dates--->test里面的数据
Map<String, String> map = gson.fromJson(jsonstr, HashMap.class);
for (String k2 : map.keySet()) {
Log.e("syp", k2 + "---2---" + map.get(k2));
}
} catch (JsonSyntaxException e) {
}
}
import java.util.Map;
public class Tests {
private String productcode;
private String productname;
private Map<String, Object> dates;//此处是Object类型
//set get....
}