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

从创建您自己的自定义对象News开始,它具有所有属性 id、title、image...

然后在解析 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");
}

欲了解更多信息,请访问:

于 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

使用JSONObjectJSONArray类。

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 回答