我试图将我从Json解析的数据显示到带有SimpleAdapter的Listview中。Android JsonArray无法检索所有值并在ListView中使用SimpleAdapter显示
我可以显示除JsonArray中的多个值存在问题之外的所有数据。
JSON数据
{
"57": {
"Steak Doness": [
"well done",
"medium",
"rare"
]
},
"58": {
"Size": [
"small",
"medium",
"large",
"extra large"
]
},
"59": {
"Cooking Method": [
"spicy",
"non-spicy"
]
},
"63": {
"Coldness": [
"with ice",
"without ice"
]
}
}
Java编码
JSONObject jsonObject = new JSONObject(response);
String get_option_data=jsonObject.getString("option_data");
JSONObject jsonObject1 = new JSONObject(get_option_data);
Iterator<String> iterator = jsonObject1.keys();
while (iterator.hasNext()) {
String oid = iterator.next();
String name_value=jsonObject1.getString(oid);
JSONObject jsonObject2=new JSONObject(name_value);
Iterator<String> iterator1 = jsonObject2.keys();
while (iterator1.hasNext()) {
String name=iterator1.next();
HashMap<String, String> map = new HashMap<>();
map.put("oid",oid);
map.put("name",name);
JSONArray jsonArray=jsonObject2.getJSONArray(name);
int i;
for(i=0;i<jsonArray.length();i++){
map.put("value",jsonArray.getString(i));
}
aList.add(map);
}
}
String[] from = {"oid","name","value"};
int[] to = {R.id.oid,R.id.name,R.id.value};
SimpleAdapter adapter = new SimpleAdapter(option.this, aList, R.layout.option_template, from, to);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
正如你所看到的,它只能显示JsonArray的最后一个元素,但不是每一个元素那个JsonArray。任何人都知道什么是错的?
到目前为止它工作正常。显示所有元素。然后,对于“在你的adpater中,你必须使用split(”,“)创建数组并使用它。”你能为这部分提供编码吗?由于所有的价值都粘在一起,没有空间 – gosulove
@gosulove:现在检查 – Jai
真棒!它现在正常工作thx – gosulove