android中對json數據的解析,並在listview中現實,下面是數據
{"ziparea": "410100.0", "enddate": "2015-04-03 00:00:00", "ecertarea": "\u9053\u8def\u8d27\u7269\u8fd0\u8f93\u9a7e\u9a76\u5458", "ecertstate": "\u4ece\u4e1a", "begindate": "2009-04-03 18:12:30", "sex": "\u7537", "birthday": "1958-06-25 00:00:00", "result": 0, "ecertid": "4101010020009000363", "addr": "\u90d1\u5dde\u5e02\u4e2d\u539f\u533a\u4f0f\u725b\u5357\u8def\u79e6\u5cad\u5c0f\u533a4\u53f7\u697c54\u53f7", "name": "\u5b54\u7965\u6c11"}java
最好先定義一個實體類(A.class)對應數據。
若是你的josn是用默認的josn格式就這樣
JosnArray list=JosnArray(「你上面的josn數據」);
List<A> l=new ArrayList<A>();
A a;
for(JsonObject o:list)
{
a=new A();
a.setXXX(o.getString("這裏json裏面對應的建"))
a.setXXX(o.getString("這裏json裏面對應的建"))
l.add(a);
}
這樣List<A> l裏面就裝好了數據了直接 A.getxxx()就ok
首先 你的json數據還應該加個key以下
{"info":[{"ziparea": "410100.0", "enddate": "2015-04-03 00:00:00", "ecertarea": "\u9053\u8def\u8d27\u7269\u8fd0\u8f93\u9a7e\u9a76\u5458", "ecertstate": "\u4ece\u4e1a", "begindate": "2009-04-03 18:12:30", "sex": "\u7537", "birthday": "1958-06-25 00:00:00", "result": 0, "ecertid": "4101010020009000363", "addr": "\u90d1\u5dde\u5e02\u4e2d\u539f\u533a\u4f0f\u725b\u5357\u8def\u79e6\u5cad\u5c0f\u533a4\u53f7\u697c54\u53f7", "name": "\u5b54\u7965\u6c11"}]}
接着定義一個entity
好比
android
public class Info { private int XX= 0; private String XXX = null; private int XXXX= 0; }
接着解析
方法以下
json
public static List<Info> getInfos(String key, String jsonString) { List<Info> list = new ArrayList<Info>(); try { JSONObject jsonObject = new JSONObject(jsonString); // 返回json的數組 JSONArray jsonArray = jsonObject.getJSONArray(key); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject2 = jsonArray.getJSONObject(i); Info info = new Info(); info .setXX(jsonObject2.getInt("XX")); info .setXXX(jsonObject2.getString("XXX")); info .setXXX(jsonObject2.getInt("XXXX")); list.add(info); } } catch (JSONException e) { e.printStackTrace(); } return list; }
上面代碼中key就爲json數據中添加的key數組