{
"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有人告诉我一个简单的方法。任何帮助都非常感谢。请大家。
答案 0 :(得分: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;
}
答案 1 :(得分: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");
}
有关详细信息,请转到:
答案 2 :(得分:0)
http://www.vogella.com/articles/AndroidJSON/article.html
JSONArray jsonArray = new JSONArray(readTwitterFeed);.....
看到这个简单的例子...... 这可能会对你有所帮助
答案 3 :(得分: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
答案 4 :(得分: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包围。