解析JSON到的ListView [英] PARSing JSON to ListView
问题描述
好吧,我已经通过吨的例子在分析我的JSON的结果,没有运气去了。我有以下的JSON例子,我不想状态信息或地理位置现在。我只是想用站对象并选择从阵列有些东西在我的ListView作为一个列表中显示。我不明白,任何在那里的文档。
会有人能够提供关于如何读取JSON并把它到ListView一个简单的例子。这将是非常有益的。我真的没有任何code组成,因为它没有一个真正为我工作。
{
状态: {
错误:否,
code:200,
说明:无,
消息:请求确认
},
地理位置:{
city_id:147,
city_long:圣洛朗,
region_short:QC
region_long:魁北克,
country_long:加拿大
COUNTRY_ID:43,
REGION_ID:35
},
站:
{
国:加拿大
reg_price:N \ / A,
mid_price:N \ / A,
pre_price:N \ / A,
diesel_price:N \ / A,
地址:3885,大道德拉ç\ u00f4te-的Vertu,
柴油:0,
ID:33862,
纬度:45.492367
液化天然气:-73.710915
站:壳牌,
标识:HTTP:\ / \ / www.mygasfeed.com \ / IMG \ /站标识\ /logo-shell.png
区域:魁,
城市:圣洛朗,
reg_date:N \ / A,
mid_date:N \ / A,
pre_date:N \ / A,
diesel_date:N \ / A,
距离:1.9公里
}
]
下面是你将如何解析数组和列表视图中显示它的一个例子。我并没有包括XML,但它是pretty的简单描述包含的国家和地址(或其他)txt的字段列表视图和多行的结果:
的String []从=新的String [] {row_1,row_2};
INT []到=新INT [] {R.id.country,R.id.address};
名单< HashMap的<字符串,字符串>> fillMaps =新的ArrayList< HashMap的<字符串,字符串>>();
尝试 {
JSONObject的OBJ =新的JSONObject(jsonString);
JSONArray站= obj.getJSONArray(站);
Log.i(TAG条目数+ stations.length());
对于(INT J = 0; J< stations.length(); J ++){
的JSONObject的JSONObject = stations.getJSONObject(J);
HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
map.put(row_1,jsonObject.getString(国家));
map.put(row_2,jsonObject.getString(地址));
fillMaps.add(图)
}
}赶上(例外五){
e.printStackTrace();
}
SimpleAdapter适配器=新SimpleAdapter(背景下,fillMaps,R.layout.result,从,到);
mListView.setAdapter(适配器);
Okay I have went through tons of examples on PARSing my JSON results, with no luck. I have the below JSON example I don't want status info or geoLocation right now. I just want to use stations object and pick some things from the array to display in my ListView as a list. I don't understand any of the documentation out there.
Would someone be able to provide a simple example on how read the JSON and put it to a ListView. This would be very helpful. I don't really have any code made up since none of it really worked for me.
{
"status": {
"error": "NO",
"code": 200,
"description": "none",
"message": "Request ok"
},
"geoLocation": {
"city_id": "147",
"city_long": "Saint-Laurent",
"region_short": "QC",
"region_long": "Quebec",
"country_long": "Canada",
"country_id": "43",
"region_id": "35"
},
"stations": [
{
"country": "Canada",
"reg_price": "N\/A",
"mid_price": "N\/A",
"pre_price": "N\/A",
"diesel_price": "N\/A",
"address": "3885, Boulevard de la C\u00f4te-Vertu",
"diesel": "0",
"id": "33862",
"lat": "45.492367",
"lng": "-73.710915",
"station": "Shell",
"logo": "http:\/\/www.mygasfeed.com\/img\/station-logo\/logo-shell.png",
"region": "Quebec",
"city": "Saint-Laurent",
"reg_date": "N\/A",
"mid_date": "N\/A",
"pre_date": "N\/A",
"diesel_date": "N\/A",
"distance": "1.9km"
}
]
Here's an example of how you would parse the array and display it in the listview. I didn't include the xml but it is pretty simple to describe the listview and multi-line result containing the txt fields for country and address (or whatever):
String[] from = new String[] {"row_1", "row_2"};
int[] to = new int[] { R.id.country, R.id.address};
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
try {
JSONObject obj = new JSONObject(jsonString);
JSONArray stations = obj.getJSONArray("stations");
Log.i(TAG,"Number of entries " + stations.length());
for (int j = 0; j < stations.length(); j++) {
JSONObject jsonObject = stations.getJSONObject(j);
HashMap<String, String> map = new HashMap<String, String>();
map.put("row_1", jsonObject.getString("country"));
map.put("row_2", jsonObject.getString("address"));
fillMaps.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
SimpleAdapter adapter = new SimpleAdapter(context, fillMaps, R.layout.result, from, to);
mListView.setAdapter(adapter);
这篇关于解析JSON到的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!