[{"name":"音箱狂欢",imgUrl":"http://7mno4h.com2.z0.glb.qiniucdn.com/5608f3b5Nc8d90151.jpg","type":1},{"name":"手机国庆礼","imgUrl":"http://7mno4h.com2.z0.glb.qiniucdn.com/5608eb8cN9b9a0a39.jpg","type":1}]
// 解析json方式:遇到{}就用JsonObject, 遇到[]就用JsonArray 如果json数据最外层是[]不是{} 用gson.jar解析就需要注意了
. 在使用Gson解析json字符串的时候,怎么建立JavaBean 是非常关键的。要注意一下三点,
第一. 内部嵌套的类必须是static的,要不然解析会出错。
第二. 类里面的属性名必须跟json字段里面的key是一模一样的。
第三. 内部嵌套的用[], 括起来的部分是一个List, 所以定义为public List<B> b. 而只用{}嵌套的就定义为public C c;
请看如下代码:
json字符串 :
String json = { "a":"100", "b":[{"b1":"b_value1","b2":"b_value2"}, {"b1":"b_value1","b2":"b_value2"}], "c": {"c1":"c_value1","c2":"c_value2"} }
方式一:原生解析法
ArrayList<> mBanners = new ArrayList<>();
try {
JSONArray jsonArray = new JSONArray(string);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
BannerBean bean = new BannerBean();
bean.imgUrl = jsonObject.getString("imgUrl");
bean.name = jsonObject.getString("name");
mBanners.add(bean);
}
} catch (JSONException e) {
e.printStackTrace();
}
方式二:gson.jar解析法
Gson gson = new Gson();
Type listType = new TypeToken<List<BannerBean2>>() {}.getType();
ArrayList<BannerBean2> list = gson.fromJson(string, listType);
for (int i = 0; i < list.size(); i++) {
BannerBean2 bannerBean2 = new BannerBean2();
bannerBean2.name = list.get(i).name;
Log.i(TAG, "name>>" + bannerBean2.name);
}
for (BannerBean2 bean2 : list) {
Log.i(TAG, "name=====>>" + bean2.name);
BannerBean2 b2= new BannerBean2();
b2.name = bean2.name;
Log.i(TAG, "name=====>>" + b2.name);
}
Type listType2 = new TypeToken<LinkedList<BannerBean2>>() {}.getType();
LinkedList<BannerBean2> list2 = gson.fromJson(string, listType2);
for (Iterator iterator = list2.iterator(); iterator.hasNext(); ) {
BannerBean2 bean2 = (BannerBean2) iterator.next();
Log.i(TAG, "bean2name>>" + bean2.name);
}
public class BannerBean2 {
public String name;
public String imgUrl;
}
Gson解析json,单独以及数组 这样的json数据,String myjson 如下:[{"id":"3","title":"\u56fe\u72473","pic":"\/Uploads\/201311\/5281ef5bdb576.jpg","lang":"1"},{"id":"2","title":"\u56fe\u72472","pic":"\/Uploads\/201402\/52fb27b0f08b0.jpg","lang":"1"},{"id":"1","title":"\u56fe\u72471","pic":"\/Uploads\/201402\/52fb27d992890.jpg","lang":"1"}]先建一个model:package com.zztt.model;public class ImageInfo { private String id; private String title; private String pic; private String lang; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getPic() { return pic; } public void setPic(String pic) { this.pic = pic; } public String getLang() { return lang; } public void setLang(String lang) { this.lang = lang; } } 然后,导包,不要倒错了 import java.lang.reflect.Type;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken; 单个的这样:Gson gson = new Gson();ImageInfo imageInfo = gson.fromJson(myjson, ImageInfo.class); 数组这样:Type listType = new TypeToken<ArrayList<ImageInfo>>() { }.getType(); Gson gson = new Gson(); ArrayList<ImageInfo> imageInfo = gson.fromJson(myjson, listType);
FastJson解析is开头变量方法
解析序列化对象的 is开头的变量 取值一直为false的解决方案 在转换实体Bean的时候我们通常会借助GsonFormat 自动转换Bean如下实体
public void setSupport(boolean support) {
isSupport = support;
}
//FastJson必须改成这样 才能正确解析
public void setIsSupport(boolean support) {
isSupport = support;
}