2012-04-30 20 views
1
{ 
    "TopNews": [{ 
     "id": "5", 
     "title": "http:\/\/Test\/news_images\/title5", 
     "image": "http:\/\/Test\/news_images\/image5", 
     "description": "desc5", 
     "canComment": true 
    }, { 
     "id": "4", 
     "title": "http:\/\/Test\/news_images\/title4", 
     "image": "http:\/\/Test\/news_images\/image4", 
     "description": "desc4", 
     "canComment": true 
    }, { 
     "id": "3", 
     "title": "http:\/\/Test\/news_images\/title3", 
     "image": "http:\/\/Test\/news_images\/image3", 
     "description": "desc3", 
     "canComment": true 
    }, { 
     "id": "2", 
     "title": "http:\/\/Test\/news_images\/title2", 
     "image": "http:\/\/Test\/news_images\/image2", 
     "description": "description2", 
     "canComment": true 
    }, { 
     "id": "1", 
     "title": "http:\/\/Test\/news_images\/title1", 
     "image": "http:\/\/Test\/news_images\/image1", 
     "description": "desc1", 
     "canComment": true 
    }] 
} 

我想讀並顯示在我的應用程序是什麼「ID」 tag.please有人告訴我一個簡單的way.Any的幫助是非常appreciated.please傢伙。讀簡單的JSON在本地主機上的Android

+0

http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/ – Anirudh

+0

謝謝Anirudh.but有一個問題.JSON數據在我的本地machine.is這種方法適合localhost呢? ?? – bynu022

+0

你能解釋一下嗎?你有一個Android應用程序正在讀取本地主機上的數據,這是你的機器或設備本身你是否已經將json存儲爲本地文件,或者是否正在向正在提供此數據的計算機(本地主機)發出http調用?無論哪種情況,解析都是一樣的。 – Anirudh

回答

2

通過創建自己的自定義對象個新聞擁有所有的屬性ID,標題,圖像...

然後填寫該對象在解析JSON對象如下:

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

JSONObject json = getJSONfromURL("localhost/SericeReturnsJSONObject.whatEver"); 

try 
{ 
    ArrayList<News> alNewsListfromJson = alGetNewsList(json); 
} 
catch (JSONException e) 
{ 
    e.printStackTrace(); 
} 

private ArrayList<News> alGetNewsList(JSONObject json) throws JSONException { 

    JSONArray TopNews = json.getJSONArray("TopNews"); 
    News oNews = null; 
    ArrayList<News> alNewslist = new ArrayList<News>(); 

    for (int i = 0; i < TopNews.length(); i++) { 

     JSONObject oJSONObject = shows.getJSONObject(i); 
     oNews = new News(); 

     oNews.setNewsID(oJSONObject.getString("id"));  
     oNews.setNewsTitle(oJSONObject.getString("title")); 
     // .. etc 

     alNewslist.add(oNews); 
     oNews = null; 
    } 
return alNewslist; 
} 
0
JSONObject jsonObject = new JSONObject(payload); // payload is your JSON 
JSONArray my_news = jsonObject.getJSONArray("TopNews"); 

List<int> my_ids = new ArrayList<int>(); 

for (int i = 0; i < my_news.length(); i++) { 
    JSONObject my_object = my_news.getJSONObject(i); 
    int id = Integer.parseInt(my_object.getString("id")); 
    my_ids.push(id); 
} 

別當需要時,忘記圍繞try/catch。

相關問題