Fastjson是一个阿里开源出来的一个Java语言编写的高性能功JSON库,基本使用介绍如下:
POM:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>
1.1
.
41
</version>
</dependency>
|
以崩溃后台的查询为例:
1、崩溃后台查询的返回如下:
{"statistic_data":{"total_num":"xxx","crash_hash_num":"xxx","mid_num":"xxx"},"table_data":[{"crash_hash":"xxxxxxxxxxxxxxxxx","version":"x.x.x.xxx","first_time":"1433501742","last_time":"1433865273","assigned_module":"xxx.xxxx.xxxx","assigned_group":"xxx","num":"xxx","mid_num":"xxxx"},{……},……],"pageNum":"0"}
|
2、从返回内容可以看出,是一个比较标准的json串,所以根据json串格式,定义如下几个类:
CrashQueryResult.java
import
java.util.ArrayList;
import
java.util.List;
public
class
CrashQueryResult {
private
CrashQueryInfo statistic_data;
private
ArrayList<CrashHashElement> table_data =
new
ArrayList<CrashHashElement>();
private
String pageNum;
public
CrashQueryInfo getStatistic_data() {
return
statistic_data;
}
public
void
setStatistic_data(CrashQueryInfo statistic_data) {
this
.statistic_data = statistic_data;
}
public
ArrayList<CrashHashElement> getTable_data() {
return
table_data;
}
public
void
setTable_data(ArrayList<CrashHashElement> table_data) {
this
.table_data = table_data;
}
public
String getPageNum() {
return
pageNum;
}
public
void
setPageNum(String pageNum) {
this
.pageNum = pageNum;
}
@Override
public
String toString() {
return
"CrashQueryResult [statistic_data="
+ statistic_data
+
", table_data="
+ table_data +
", pageNum="
+ pageNum +
"]"
;
}
}
|
CrashQueryInfo.java
import
java.util.ArrayList;
public
class
CrashQueryInfo {
private
String total_num;
private
String crash_hash_num;
private
String mid_num;
public
String getTotal_num() {
return
total_num;
}
public
void
setTotal_num(String total_num) {
this
.total_num = total_num;
}
public
String getCrash_hash_num() {
return
crash_hash_num;
}
public
void
setCrash_hash_num(String crash_hash_num) {
this
.crash_hash_num = crash_hash_num;
}
public
String getMid_num() {
return
mid_num;
}
public
void
setMid_num(String mid_num) {
this
.mid_num = mid_num;
}
@Override
public
String toString() {
return
"CrashQueryInfo [total_num="
+ total_num +
", crash_hash_num="
+ crash_hash_num +
", mid_num="
+ mid_num +
"]"
;
}
}
|
CrashHashElement.java
public
class
CrashHashElement {
// 查询crash hash
private
String crash_hash;
// 查询版本
private
String version;
// 查询开始时间
private
String first_time;
// 查询截止时间
private
String last_time;
// 崩溃堆栈模块
private
String assigned_module;
// 崩溃所属模块
private
String assigned_group;
// 崩溃次数
private
String num;
// 用户数
private
String mid_num;
public
String getCrash_hash() {
return
crash_hash;
}
public
void
setCrash_hash(String crash_hash) {
this
.crash_hash = crash_hash;
}
public
String getVersion() {
return
version;
}
public
void
setVersion(String version) {
this
.version = version;
}
public
String getFirst_time() {
return
first_time;
}
public
void
setFirst_time(String first_time) {
this
.first_time = first_time;
}
public
String getLast_time() {
return
last_time;
}
public
void
setLast_time(String last_time) {
this
.last_time = last_time;
}
public
String getAssigned_module() {
return
assigned_module;
}
public
void
setAssigned_module(String assigned_module) {
this
.assigned_module = assigned_module;
}
public
String getAssigned_group() {
return
assigned_group;
}
public
void
setAssigned_group(String assigned_group) {
this
.assigned_group = assigned_group;
}
public
String getNum() {
return
num;
}
public
void
setNum(String num) {
this
.num = num;
}
public
String getMid_num() {
return
mid_num;
}
public
void
setMid_num(String mid_num) {
this
.mid_num = mid_num;
}
@Override
public
String toString() {
return
"CrashHashElement [crash_hash="
+ crash_hash +
", version="
+ version +
", first_time="
+ first_time +
", last_time="
+ last_time +
", assigned_module="
+ assigned_module
+
", assigned_group="
+ assigned_group +
", num="
+ num
+
", mid_num="
+ mid_num +
"]"
;
}
}
|
3、将字符串信息转化为我们定义的json对象
public
void
parseStringToObj(String strInput)
{
CrashQueryResult crashQueryResult = JSON.parseObject(strInput, CrashQueryResult.
class
);
}
|
注意:
主要成员变量的命名方式需要和json串中的命名一致,并且以此来生成对应的set和get方法