使用具有动态密钥的GSON解析JSON响应 [英] Parsing JSON response using GSON with dynamic Key
本文介绍了使用具有动态密钥的GSON解析JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的JSON的样子,我必须解析JSON,如何使用GSON来完成
This is how my JSON looks like and I have to parse the JSON, how can this be done using GSON
{
"data": {
"a": {
"abc": {
"c": "d"
}
}
}
}
其中"a"是动态键,可能会不时变化.我现在找不到解决方法
In which "a" is dynamic key which may vary from time to time. I am unable to find a solution right now
推荐答案
模型
public class Model {
private HashMap<String, String> data;
public Model() {
}
}
使用Gson&将JSON字符串转换为Hashmap.从哈希图准备数据
Gson gson = new Gson();
Type typeHashMap = new TypeToken<Map<String, String>>(){}.getType();
Map<String,String> map = gson.fromJson(YOUR_JSON, typeHashMap);
Set<Map.Entry<String, String>> entrySet = data.entrySet();
Iterator iterator = entrySet.iterator ();
for(int j = 0; j < entrySet.size(); j++) {
try {
Map.Entry entry = (Map.Entry) iterator.next();
String key = entry.getKey().toString();
String value = entry.getValue().toString();
//Add it to your list
}
catch(NoSuchElementException e) {
e.printStackTrace();
}
break;
}
这篇关于使用具有动态密钥的GSON解析JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文