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」タグです。誰か簡単な方法を教えてください。どんな助けでも大歓迎です。

4

5 に答える 5

2

まず、すべての属性id、title、image...を持つ独自のカスタムオブジェクトNewsを作成します。

次に、次のように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;
}
于 2012-04-30T09:49:11.900 に答える
1
JSONObject fullJObj = new JSONObject(myStr);
JSONArray jArr = fullJObj.getJSONArray("TopNews");
for (int i = 0; i < jArr.length(); ++i ) {
    JSONObject jObj = jArr.getJSONObject(i);
    String id = jObj.getString("id");
}

詳細については、次のURLにアクセスしてください。

于 2012-04-30T09:47:34.270 に答える
0

http://www.vogella.com/articles/AndroidJSON/article.html

JSONArray jsonArray = new JSONArray(readTwitterFeed);.....

この簡単な例を参照してください...それはあなたを助けるかもしれません

于 2012-04-30T09:47:07.390 に答える
0

JSONObjectクラスとJSONArrayクラスを使用します。

http://developer.android.com/reference/org/json/JSONObject.html

http://developer.android.com/reference/org/json/JSONArray.html

ウェブはAndroidの簡単な例でクロールしています。

基本的にあなたがする必要があるのは:

  • 文字列をJSONObjectにロードします

  • TopNewsからJSONArrayを作成します

  • それを通過し、IDを取得します

于 2012-04-30T09:47:38.607 に答える
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 で囲むことを忘れないでください。

于 2012-04-30T10:01:04.107 に答える