Fastjson是一个Java语言编写的高性能功能完善的JSON库。fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jackson
利用fastjson能利用对象快速的匹配内容,不必再像org.json一样,逐步解析,下面来分析一下这个例子:
json字符串数据模型如下图:
由此只用梳理一下层级关系,以此来定义对应的对象就好了,
由此定义了4个对象:JsonHead,PicSetBean,PicUrl,PicUrlStr其中属性根据结构决定
定义对象时应小心对应定义,如果没有定义正确,将无法解析
public class JsonHead {
private PicSetBean info; //属性字符串对应(info)
public PicSetBean getInfo() {
return info;
}
public void setInfo(PicSetBean info) {
this.info = info;
}
}
public class PicSetBean {
<span style="white-space:pre"> </span>//由于图片上显示picSet下面内容为数组形式,因此定义为数组
private ArrayList<PicUrl> picSet; //定义picSet应与图中字符相对应
public ArrayList<PicUrl> getPicSet() { //<span style="font-family: 微软雅黑, 黑体, sans-serif;">PicUrl为下一级对象</span>
return picSet;
}
public void setPicSet(ArrayList<PicUrl> picSet) {
this.picSet = picSet;
}
}
public class PicUrl {
private ArrayList<PicUrlStr> picUrlSet;
private String albumsName;
public ArrayList<PicUrlStr> getPicUrlSet() {
return picUrlSet;
}
public void setPicUrlSet(ArrayList<PicUrlStr> picUrlSet) {
this.picUrlSet = picUrlSet;
}
public String getAlbumsName() {
return albumsName;
}
public void setAlbumsName(String albumsName) {
this.albumsName = albumsName;
}
}
public class PicUrlStr {
private String picUrl;
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
}
下面看下测试代码:
<font size="3"><span style="font-family:微软雅黑, 黑体, sans-serif;color:#313131;"><span style="font-family:微软雅黑, 黑体, sans-serif;color:#313131;"><span style="line-height: 22px; white-space: pre;"> </span><span style="line-height: 22px;">InputStream is = getContext().getAssets().open("json_xx"); //通过流来读取assets中的文件
Reader reader = new InputStreamReader(is,"UTF-8"); //为了尽量不乱码,此处尽量加上utf-8
BufferedReader br = new BufferedReader(reader);
String str = null;
StringBuffer sb = new StringBuffer();
while((str = br.readLine())!= null){
sb.append(str);
}
JsonHead Root = JSONObject.parseObject(sb.toString(), JsonHead.class); //将文件中读出的数据解析为JsonHend对象
PicSetBean info = Root.getInfo(); //逐级获取信息
ArrayList<PicUrl> picSet = info.getPicSet();
for (PicUrl picUrl : picSet) { //有多项数据时,遍历获取
String picName = picUrl.getAlbumsName();
Logs.e("picName--"+picName);
ArrayList<PicUrlStr> picStr = picUrl.getPicUrlSet();
for (Iterator iterator = picStr.iterator(); iterator.hasNext();) { //picUrlSet中数组数据,遍历取出
PicUrlStr string = (PicUrlStr) iterator.next();
String pic = string.getPicUrl();
Logs.d("pic--"+pic); //</span></span></span>此时打印picUrl所对应的数据<span style="font-family:微软雅黑, 黑体, sans-serif;color:#313131;"><span style="font-family:微软雅黑, 黑体, sans-serif;color:#313131;"><span style="line-height: 22px;">
}
}
}</span></span></span></font>
利用fastjson生成json字符串:
此处就利用一下上面PicUrlStr对象来简单介绍一下
<span style="font-size:14px;">PicUrlStr pic = new PicUrlStr();</span>
<span style="font-size:14px;"> pic.setPicUrl("生成json");</span>
<span style="font-size:14px;">String fastJson = JSONObject.toJSONString(pic);
<span style="white-space: pre;"> </span>Log.d("test", fastJson); //log打印出来符合json字符串要求 其中key值为对象中属性值<span style="font-family:微软雅黑, 黑体, sans-serif;color:#313131;"><span style="line-height: 22px; background-color: rgb(240, 240, 240);">
</span></span></span>