您的位置:首页 > 理论基础 > 计算机网络

利用FastJson来解析网络数据

2016-06-21 23:01 387 查看
FastJson:阿里巴巴

主要特点:

快速FAST (比其它任何基于Java的解析器和生成器更快,包括jackson)

强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum)

零依赖(没有依赖其它任何类库除了JDK)

java语言最快的json库fastjson发布1.1.32版本,这个版本增加了stream api,支持处理超大json文本

首先需要添加依赖,



我们根据这个知乎的数据在Json工具下解析,



创建实体类,

**
*  根实体类
*/
public class RootBean {

private long limit;
private List<InfoBean> others;

public long getLimit() {
return limit;
}

public void setLimit(long limit) {
this.limit = limit;
}

public List<InfoBean> getOthers() {
return others;
}

public void setOthers(List<InfoBean> others) {
this.others = others;
}
}


public class InfoBean {

private int color;
private String thumbnail;
private String description;
private int id;
private String name;

public int getColor() {
return color;
}

public void setColor(int color) {
this.color = color;
}

public String getThumbnail() {
return thumbnail;
}

public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}


获取数据,并使用,

public class MainActivity extends AppCompatActivity {

//知乎的数据
private String url = "http://news-at.zhihu.com/api/4/themes";
private RequestQueue queue;
private TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv = (TextView) this.findViewById(R.id.tv);
queue = Volley.newRequestQueue(this);

StringRequest sr = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
tv.setText(response);
//解析 知乎接口
RootBean rootBean = JSON.parseObject(response,RootBean.class);
//获取解析结果集合
List<InfoBean> datas = rootBean.getOthers();
//就可以设置到适配器了
tv.setText(datas.get(0).getDescription());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});
//加入队列
queue.add(sr);
}
}


不仅能解析以上的数据,还可以解析value是文字的数据,



利用GsonFormat来解析数据,并创建数据类,



接下来,直接使用就可以了,

public class MainActivity extends AppCompatActivity {

//段子的数据
String url = "http://c.3g.163.com/recommend/getChanListNews?passport=&devId=866907021173843&size=20&channel=duanzi";
private TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv = (TextView) this.findViewById(R.id.tv);

RequestQueue requestQueue = Volley.newRequestQueue(this);

StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//解析段子的数据
DuanZiBean duanZiBean = JSON.parseObject(response, DuanZiBean.class);
//可以获取value的数据
tv.setText(duanZiBean.get段子().get(0).getDigest());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});
requestQueue.add(stringRequest);
}
}


以上就是利用FastJson来解析网络数据并简单使用,

本人菜鸟一个,有什么不对的地方希望大家指出评论,大神勿喷,希望大家一起学习进步!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: