此博文以下列的json数据为例,讲解json转换或解析经常遇到的问题。
{
"success": true,
"resultMessage": "",
"result": [{
"code": "01",
"level": 1,
"orderSort": 1,
"pcode": "0",
"name": "配件",
"isleaf": 0,
"attrs": [{
"param_code": "01",
"sku": null,
"param_name": "支腿",
"param_value": "0-1000个",
"params_type": 0,
"unit": "个",
"sort": 1,
"type": 0,
"category_group": "结构件",
"group_sort": 3,
"note": "配件类型中的支腿"
}, {
"param_code": "02",
"sku": null,
"param_name": "主电缆",
"param_value": "0-100米",
"params_type": 0,
"unit": "米",
"sort": 2,
"type": 0,
"category_group": "电气件",
"group_sort": 3,
"note": "配件类型中的主电缆"
}]
}],
"resultCode": "5000"
}
此json数据涉及三个实体类,由外到内的顺序分别是
JsonRootBean–>Attrs–>Result
JsonRootBean.java
/**
* Copyright 2019 bejson.com
*/
package com.besjon.pojo;
import java.util.List;
/**
* Auto-generated: 2019-01-08 17:30:27
*
* @author bejson.com (i@bejson.com)
* @website http://www.bejson.com/java2pojo/
*/
public class JsonRootBean {
private boolean success;
private String resultMessage;
private List<Result> result;
private String resultCode;
public void setSuccess(boolean success) {
this.success = success;
}
public boolean getSuccess() {
return success;
}
public void setResultMessage(String resultMessage) {
this.resultMessage = resultMessage;
}
public String getResultMessage() {
return resultMessage;
}
public void setResult(List<Result> result) {
this.result = result;
}
public List<Result> getResult() {
return result;
}
public void setResultCode(String resultCode) {
this.resultCode = resultCode;
}
public String getResultCode() {
return resultCode;
}
}
Attrs.java
/**
* Copyright 2019 bejson.com
*/
package com.besjon.pojo;
/**
* Auto-generated: 2019-01-08 17:30:27
*
* @author bejson.com (i@bejson.com)
* @website http://www.bejson.com/java2pojo/
*/
public class Attrs {
private String param_code;
private String sku;
private String param_name;
private String param_value;
private int params_type;
private String unit;
private int sort;
private int type;
private String category_group;
private int group_sort