{
success: true,
message: "xxxx",
total: 200,
ajList: [{
bh: "xxxx",
ah: "2014北高民初字第0001号",
ajmc:" 张三诉李四劳动合同纠纷",
larq:" 2014-03-04",
aydm: 123,
aymc:"劳动合同纠纷",
cbts:"民一庭",
cbr:"王法官",
dsr:"张三、李四",
spzzcyList: [{
js: "审判长",
xm: "王顺"
},{
js:"审判员",
xm: "张黎"
},{
......
}]
},{
......
}]
}
这是调用接口返回来的json数据
根据数据,我创建了3个bean来存放数据
第一个bean:
public class RecordingResult {
/**
* 是否成功标识
*/
private boolean success;
/**
* 失败原因描述
*/
private String message;
/**
* 公开案件的总数
*/
private int total;
/**
* 已公开案件列表
*/
private List<AjBean> ajList;
//属性对应的get,set
}
第二个bean:
public class AjBean {
/**
* 已公开案件列表的属性
*/
private String fybh;
private String bh;// 案件主键
private String ah;//案号
private String ajmc;//案件名称
private Date larq;//立案日期
private String aydm;//案由代码
private String aymc;//案由名称
private String dsr;//当事人,多个当事人以顿号分隔
private String cbts;//承办庭室
private String cbr;//承办人
private String cbhcbr;//承办人编号
private String cbhcbts;//承办庭室编号(C_BH_CBBM)
private List<SpzzcyInfo> spzzcyList ;
//属性对应的get,set
}
第三个bean:
public class SpzzcyInfo {
/**
* 角色
*/
private String js;
/**
* 姓名
*/
private String xm;
//属性对应的get,set
}
然后就是进行解析json了:
这是解析的方法:
public static RecordingResult parseJson2List(String str){
JSONObject obj = JSONObject.fromObject(str);
RecordingResult rr = new RecordingResult();
if(obj.getBoolean("success")){
rr.setSuccess(true);
rr.setTotal(obj.getInt("total"));
JSONArray ajList = obj.getJSONArray("ajList");
List<AjBean> ab = new ArrayList<AjBean>();
for(int i=0;i<ajList.size();i++){//循环第一层list
JSONObject o = (JSONObject)ajList.get(i);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
AjBean a = new AjBean();
a.setBh(o.getString("bh"));
a.setAh(o.getString("ah"));
a.setAjmc(o.getString("ajmc"));
try {
a.setLarq(sdf.parse(o.getString("larq")));
} catch (ParseException e) {
System.out.println("接收接口传过来的数据(larq)格式不正确!");
e.printStackTrace();
}
a.setAydm(o.getString("aydm"));
a.setAymc(o.getString("aymc"));
a.setCbts(o.getString("cbts"));
a.setCbr(o.getString("cbr"));
a.setDsr(o.getString("dsr"));
if(o.containsKey("spzzcyList")){//判断是否存在第二层list
JSONArray sp = o.getJSONArray("spzzcyList");//第二层list
a.setSpzzcyList(sp);
}
ab.add(a);
}
rr.setAjList(ab);
}else{
rr.setSuccess(false);
rr.setMessage(obj.getString("message"));
rr.setAjList(null);
}
return rr;
}
这是测试用的main方法:
public static void main(String[] args) {
String str = "{ \"success\": true,\"message\": \"ss\",\"total\": 200,\"ajList\": [{\"bh\" : \"xxxx\"," +
"\"ah\" : \"2014北高民初字第0001号\","+
"\"ajmc\":\" 张三诉李四劳动合同纠纷\","+
"\"larq\":\" 2014-03-04\","+
"\"aydm\":\"123\","+
"\"aymc\" :\"劳动合同纠纷\","+
"\"cbts\":\"民一庭\","+
"\"cbr\" :\"王法官\","+
"\"dsr\" :\"张三李四\"," +
"\"spzzcyList\":[{"+
"\"js\":\"审判长\","+
"\"xm\":\"王顺\"},"+
"{"+
"\"js\":\"审判长2\","+
"\"xm\":\"王顺2\"},"+
"{"+
"\"js\":\"审判长3\","+
"\"xm\":\"王顺3\"}"+
"]"+
"},"+
"{\"bh\" : \"xxxx\","+
"\"ah\" : \"2014北高民初字第0002号\","+
"\"ajmc\":\"张三诉李四劳动合同纠纷\","+
"\"larq\":\" 2014-03-04\","+
"\"aydm\":\"1234\","+
"\"aymc\" :\"劳动合同纠纷1\","+
"\"cbts\":\"民一庭1\","+
"\"cbr\" :\"王法官1\","+
"\"dsr\" :\"张三、李四1\"}]} ";
parseJson2List(str);
}
完事!