当前位置: 代码迷 >> Android >> 如何从集合中取出特定集合
  详细解决方案

如何从集合中取出特定集合

热度:76   发布时间:2016-04-28 05:52:00.0
怎么从集合中取出特定集合
一下为我从接口获得的数据,我想把id为1的数据拿出来,怎么实现?接口没有根据id查询数据的功能。。。
{"content":[{"id":"1","tittle":"1","content":"1","creattime":"1","isread":"1","mdn":"1"},
{"id":"2","tittle":"2","content":"2","creattime":"2","isread":"2","mdn":"2"}],"size":2,"number":0,"sort":[{"direction":"ASC","property":"creattime","ascending":true}],"totalPages":2,"numberOfElements":2,"totalElements":3,"firstPage":true,"lastPage":false}

------解决方案--------------------
这个算是android的json解析吧。

String strJson = "{\"content\":[{\"id\":\"1\",\"tittle\":\"1\",\"content\":\"1\",\"creattime\":\"1\",\"isread\":\"1\",\"mdn\":\"1\"},"
+ "{\"id\":\"2\",\"tittle\":\"2\",\"content\":\"2\",\"creattime\":\"2\",\"isread\":\"2\",\"mdn\":\"2\"}],"
+ "\"size\":2,\"number\":0,\"sort\":"
+ "[{\"direction\":\"ASC\",\"property\":\"creattime\",\"ascending\":true}],"
+ "\"totalPages\":2,\"numberOfElements\":2,\"totalElements\":3,\"firstPage\":true,\"lastPage\":false}";
try {
JSONObject jo = new JSONObject(strJson);
JSONArray jsonArray = (JSONArray) jo.get("content");
for (int i = 0; i < jsonArray.length(); ++i) {
JSONObject o = (JSONObject) jsonArray.get(i);
if ("1".equals(o.getString("id"))) {
System.out.println("id:" + o.getString("id") + ","
+ "tittle:" + o.getInt("tittle") + "," 
+ "content:"+ o.getString("content") + "," 
+ "creattime:"+ o.getString("creattime") + "," 
+ "isread:"+ o.getString("isread"));
}
}
} catch (JSONException e) {
e.printStackTrace();
}

应该没问题~你试试
  相关解决方案