获取json数据后,往往要获取需要的数据自己存放,一下面例子为例,
(一),假如返回的一个Json是这样的:
{"Success":true,"Data":[{"IID":10,"UID":"d4b8bb3577c24898982912d10a61f611","SchoolUid":"b715f6eeef0c4a6d82c0016557f61390","InformationType":3,"InformationTitle":"文本n","InformationContent":"测试文本!测试文本<br>测试文本文本!!!!","CreatedDate":"2016-12-22T00:20:22.297","UpdateDate":"2016-12-22T00:20:22.297","CreateUser":null,"UpdateUser":null,"IsRemove":0,"IsActive":1,"DeviceUID":"b763ec53f4b0411090885805b1edf9c1","DisplayOrder":2,"IntervalNum":1},{"IID":9,"InformationType":2,"InformationTitle":"视频","InformationContent":"http://images.qipeilong.cn/oldupload/8ec03d31-e0b4-4c89-a002-9adf6af85496.mp4",{"IID":3,"UID":"f819d9cc086a4b1baac68c62bffad203","InformationType":1,"InformationTitle":"图片","InformationContent":"http://images.qipeilong.cn/oldupload/8ec03d31-e0b4-4c89-a002-9adf6af85496.jpg,http://images.qipeilong.cn/oldupload/8ec03d31-e0b4-4c89-a002-9adf6af85496.jpg,http://images.qipeilong.cn/oldupload/8ec03d31-e0b4-4c89-a002-9adf6af85496.jpg","CreatedDate":"2016-12-22T00:18:55.557","UpdateDate":"2016-12-22T00:18:55.557","CreateUser":"79419a8ad46043e1a8820d28bbb5bf3e","UpdateUser":"79419a8ad46043e1a8820d28bbb5bf3e","DisplayOrder":1,"IntervalNum":2}]},
(二),用网上的在线解析工具,我用的这个:http://tool.oschina.net/codeformat/json
格式化后看到是这样的数据
{ "Success": true,"Data": [ { "IID": 10,"UID": "d4b8bb3577c24898982912d10a61f611","SchoolUid": "b715f6eeef0c4a6d82c0016557f61390","InformationType": 3,"InformationTitle": "文本n","InformationContent": "测试文本!测试文本<br>测试文本文本!!!!","CreatedDate": "2016-12-22T00:20:22.297","UpdateDate": "2016-12-22T00:20:22.297","CreateUser": null,"UpdateUser": null,"IsRemove": 0,"IsActive": 1,"DeviceUID": "b763ec53f4b0411090885805b1edf9c1","DisplayOrder": 2,"IntervalNum": 1 },{ "IID": 9,"InformationType": 2,"InformationTitle": "视频","InformationContent": "http://images.qipeilong.cn/oldupload/8ec03d31-e0b4-4c89-a002-9adf6af85496.mp4",{ "IID": 3,"UID": "f819d9cc086a4b1baac68c62bffad203","InformationType": 1,"InformationTitle": "图片","InformationContent": "http://images.qipeilong.cn/oldupload/8ec03d31-e0b4-4c89-a002-9adf6af85496.jpg,"CreatedDate": "2016-12-22T00:18:55.557","UpdateDate": "2016-12-22T00:18:55.557","CreateUser": "79419a8ad46043e1a8820d28bbb5bf3e","UpdateUser": "79419a8ad46043e1a8820d28bbb5bf3e","DisplayOrder": 1,"IntervalNum": 2 } ] }
(三)
1,定义一个类,里面定义想要的数据,比如我想获取IID 和UID
public class Data { private int IID; private String UID; public int getIID() { return IID; } public void setIID(int iID) { IID = iID; } public String getUID() { return UID; } public void setUID(String uID) { UID = uID; }2,在访问请求后,返回的json里面处理:
此处List<Data> dataList;是全局变量
import com.google.gson.Gson; import com.google.gson.reflect.TypeToken;
OkHttpUtils.post() .url("http://139.224.197.151:8082/Cms/GetTopic") .params(para) .build() .execute(new StringCallback() { @Override public void onResponse(String arg0,int arg1) { // TODO Auto-generated method stub JSONObject json; try { json = new JSONObject(arg0); JSONArray FileInformationArray = json.getJSONArray("Data"); Log.i("FileInformationArray",""+FileInformationArray.toString()); Gson mGson = new Gson(); Type lisType = new TypeToken<LinkedList<Data>>(){}.getType(); LinkedList<Data> datas = mGson.fromJson(FileInformationArray.toString(),lisType); for (int i = 0; i < datas.size(); i++) { Log.i("datas",""+datas.get(i).getIID()); Log.i("datas",""+datas.get(i).getUID()); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
我的Log:
到此结束了!!!