我已经使用httpClient将json数据拉到ListView。我能够解析json数据,但不知道如何在ListView上显示这些记录 我试着跟随解析
HttpGet httpget = new HttpGet("http://10.0.2.2:8000/api/ecp/merchantinfo/?format=json");
try {
HttpEntity entity;
HttpResponse response;
response = httpclient.execute(httpget);
entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append((line + "\n"));
}
Toast.makeText(Landing.this, sb.toString(), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Xml文件如下
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow>
<TextView
android:id="@+id/tv_Land"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<TableRow>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</TableRow>
</TableLayout>
现在我需要在listview中提取名称字段 json如下
{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 1}, "objects": [{"categories": [{"api_key": "635c1a2fe3924626ddc38cc365a084d62e0201aa", "id": 1, "is_active": true, "name": "test1", "resource_uri": "/api/ecp/merchantcategory/1/"}], "contact": [{"ContactUsAddressCity": "kathmandu", "ContactUsAddressFax": "8989898", "ContactUsAddressLine1": "anamnagar32", "ContactUsAddressLine2": "kathmandu", "ContactUsAddressState": "AR", "ContactUsAddressZip": "78787", "ContactUsBusinessHours": "tetsgsasjajash nsakjaskjas nsajnjashashjsa", "ContactUsEmail": "timus2001@gmail.com", "ContactUsPhone": "78898998", "api_key": "635c1a2fe3924626ddc38cc365a084d62e0201aa", "id": 1, "resource_uri": "/api/ecp/merchantcontact/1/"}], "create_time": "2012-09-04T15:54:34", "current_state": 1, "graphics": [{"api_key": "635c1a2fe3924626ddc38cc365a084d62e0201aa", "bg": "/media/000001_background.png", "bgcolor": "#D92727", "button": "/media/000001_top.png", "fontcolor": "#1440CF", "id": 1, "photo": "/media/000001_icon.png", "resource_uri": "/api/ecp/merchantgraphics/1/", "splash": "/media/000001_splash.png"}], "id": 1, "modified_time": "2012-09-04T15:54:34", "name": "Super restro and bar", "resource_uri": "/api/ecp/merchantinfo/1/", "utcStateCreated": null, "utcStateDisabled": null, "utcStateEnabled": null, "utcStateUnsubscribed": null}]}
答案 0 :(得分:1)
获取JSON字符串后创建一个jason对象..
JSONObject object = new JSONObject(sb.trim());
JSONArray objects = object.getJSONArray("objects");
然后解析json数组......
检查链接:http://developer.android.com/reference/org/json/JSONObject.html