1. 程式人生 > >Android 解析json物件,存放到List中

Android 解析json物件,存放到List中

比如解析這段從伺服器端返回的json字串:
[{"Money":3,"EtcOutTime":"2017-5-20 15:30:22","CarId":0,"EtcInTime":"2017-5-20 15:30:22"}]
很明顯這是一個json陣列(json陣列用“[]”括起來,和Java定義方式不一樣java是“{}”),並且包含兩個大的json物件(json物件用“{}”括起來)。
1.首先我們先定義一個list
 List<Map<String,Object>> mList;
2.既然傳過來的是json陣列,所以用JsonArray節後啦,之後我們要得到具體的內容,具體內容在物件中,我們要解析他,
所以用:JSONObject temp= (JSONObject) arr.get(i) --->temp.getInt("Money")來得到具體內容。
然後放到map中然後放到list中
 JSONArray arr=new JSONArray(jsonStr);
            for (int i=0;i<arr.length();i++){
                JSONObject temp= (JSONObject) arr.get(i);
                Map<String ,Object> map=new HashMap<String, Object>();
                map.put("Money",temp.getInt("Money"));
                map.put("CarId",temp.getInt("CarId"));
                map.put("EtcOutTime",temp.getString("EtcOutTime"));
                map.put("EtcInTime",temp.getString("EtcInTime"));
                mList.add(map);
            }
3.接下來就是從list中取資料了,這裡我就不再贅述了