Gson或fastJson解析json数据的数组类型数据
1、示例:
{
"homeadlist": [{
"id": 1,
"imgurl": "/img/homead/a73b3f1d-0f14-429d-9d0f-70643fb0f0eb.jpg",
"jumpflag": 0,
"jumpurl": "",
"posflag": 1,
"remark": "1111",
"cityid": 1
}, {
"id": 12,
"imgurl": "/img/homead/eb442fbf-49db-4ba6-a102-d781505f426d.jpg",
"jumpflag": 0,
"jumpurl": "",
"posflag": 2,
"remark": "",
"cityid": 1
}, {
"id": 14,
"imgurl": "/img/homead/68109460-635d-4c5c-8be8-64d7c7889d18.jpg",
"jumpflag": 0,
"jumpurl": "http://shiranlife.kuaizhan.com/",
"posflag": 4,
"remark": "",
"cityid": 1
}]
}
2、代码如下:
1)首先生成model
public class HomeadListModel {
/**
* code : 1
* data : {"homeadlist":[{"id":1,"imgurl":"/img/homead/a73b3f1d-0f14-429d-9d0f-70643fb0f0eb.jpg","jumpflag":0,"jumpurl":"","posflag":1,"remark":"1111","cityid":1},{"id":12,"imgurl":"/img/homead/eb442fbf-49db-4ba6-a102-d781505f426d.jpg","jumpflag":0,"jumpurl":"","posflag":2,"remark":"","cityid":1},{"id":14,"imgurl":"/img/homead/68109460-635d-4c5c-8be8-64d7c7889d18.jpg","jumpflag":0,"jumpurl":"http://shiranlife.kuaizhan.com/","posflag":4,"remark":"","cityid":1}]}
*/
public int code;
public DataBean data;
public static class DataBean {
public List<HomeadlistBean> homeadlist;
public static class HomeadlistBean {
public int id;
public String imgurl;
public int jumpflag;
public String jumpurl;
public int posflag;
public String remark;
public int cityid;
}
}
}
2)使用OKhttpclient
OkHttpClient.Builder builder = new OkHttpClient.Builder();
Request request = new Request.Builder()
.url(url)
.get()
.build();
builder.build().newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Gson gson = new Gson();
/**
* java.lang.IllegalStateException: closed
* 因为OkHttp请求回调中response.body().string()只能有效调用一次
*
*/
String string = response.body().string();
Log.i("aaa", "response: " + string);
/**
* 重新构建一个response 因为response.body().string()调用一次之后流就断掉了,只能有效调用一次
*/
MediaType mediaType = response.body().contentType();
Response response1 = response.newBuilder().body(ResponseBody.create(mediaType, string)).build();
String string1 = response1.body().string();
Log.i("aaa", "response1: " + string1);
/**
* 将json数据转换为对象
*/
HomeadListModel model = gson.fromJson(string1, HomeadListModel.class);
//HomeadListModel model2 = gson.fromJson(string1, new TypeToken<HomeadListModel>() {
}.getType());
/**
* 将对象转换为json数据
*/
String jsonString = gson.toJson(model);
Log.i("aaa", "onResponse2: " + jsonString);
for (HomeadListModel.DataBean.HomeadlistBean data : model.data.homeadlist) {
mData.add(imageUrl + data.imgurl);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter.notifyDataSetChanged();
}
});
}
});
3)使用fastjson
OkHttpClient.Builder builder = new OkHttpClient.Builder();
Request request = new Request.Builder()
.url(url)
.get()
.build();
builder.build().newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String json = response.body().string();
/**
* 根据json数据获取对象
*/
HomeadListModel model = JSONObject.parseObject(json, new TypeToken<HomeadListModel>() {
}.getType());
// HomeadListModel model2 = JSONObject.parseObject(json, HomeadListModel.class);
/**
* 根据对象获取json数据
*/
String json1 = JSONObject.toJSONString(model);
Log.i("aaa", "fastJson: " + json1);
for (HomeadListModel.DataBean.HomeadlistBean data : model.data.homeadlist) {
mData.add(imageUrl + data.imgurl);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter.notifyDataSetChanged();
}
});
}
});