JSON字符串转换List对象列表 JSONArray toJavaList
package com.example.core.mydemo.java3.jsonDemo;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.core.mydemo.json2.GsonUtils;
import java.util.List;
public class Testjson {
public static void main(String[] args) {
String resultJsonData = "{\"code\":\"200\",\"message\":\"success\",\"success\":true,\n" +
"\"data\":[\n" +
"{\"countryCode\":\"CN\",\"provinceId\":1,\"provinceName\":\"北京\",\"id\":1,\"code\":\"BJS\",\"name\":\"北京\",\"isHot\":false,\"firstChar\":\"B\",\"fullPinYin\":\"Beijing\",\"shortPinYin\":\"BJ\"},\n" +
"{\"countryCode\":\"CN\",\"provinceId\":12,\"provinceName\":\"陕西\",\"id\":10,\"code\":\"SIA\",\"name\":\"西安\",\"isHot\":false,\"firstChar\":\"X\",\"fullPinYin\":\"Xian\",\"shortPinYin\":\"XA\"},\n" +
"{\"countryCode\":\"CN\",\"provinceId\":13,\"provinceName\":\"甘肃\",\"id\":100,\"code\":\"LHW\",\"name\":\"兰州\",\"isHot\":false,\"firstChar\":\"L\",\"fullPinYin\":\"Lanzhou\",\"shortPinYin\":\"LZ\"}\n" +
"]}";
BaseResponse<JSONArray> response = JSONObject.parseObject(resultJsonData, BaseResponse.class);
if(response != null && "200".equals(response.getCode())){
JSONArray data = response.getData();
// List<CityEntity> list = JSONObject.parseArray(data.toJSONString(), CityEntity.class);
List<CityEntity> list = data.toJavaList(CityEntity.class);
for (CityEntity entity:list) {
System.out.println("entity=" + GsonUtils.toJson(entity));
}
}
//以下写法错误,报错:Exception in thread "main" java.lang.ClassCastException: com.alibaba.fastjson.JSONObject cannot be cast to com.example.core.mydemo.java3.jsonDemo.CityEntity
/*BaseResponse<List<CityEntity>> response = JSONObject.parseObject(resultJsonData, BaseResponse.class);
if(response != null && "200".equals(response.getCode())){
List<CityEntity> list = response.getData();
for (CityEntity entity:list) {
System.out.println("entity=" + GsonUtils.toJson(entity));
}
}*/
}
}
package com.example.core.mydemo.java3.jsonDemo;
public class BaseResponse<T> {
String code; //返回状态
String message; //错误信息
boolean success; //是否成功
T data; //响应内容
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
package com.example.core.mydemo.java3.jsonDemo;
import java.io.Serializable;
/**
* 中国城市基本信息表
*
*/
public class CityEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 城市Id
*/
//@AutoDocProperty(value="城市Id",required=true)
private Integer id;
/**
* 省份Id
*/
//@AutoDocProperty(value="省份Id",required=true)
private Integer provinceId;
/**
* 省份名称
*/
//@AutoDocProperty(value="省份名称",required=true)
private String provinceName;
/**
* 城市编码
*/
//@AutoDocProperty(value="城市编码",required=true)
private String code;
/**
* 城市名称
*/
//@AutoDocProperty(value="城市名称",required=true)
private String name;
/**
* 是否热门城市
*/
//@AutoDocProperty(value="是否热门城市",required=true)
private Integer isHot;
/**
* 城市名称首字母
*/
//@AutoDocProperty(value="城市名称首字母",required=true)
private String firstChar;
/**
* 全拼音
*/
//@AutoDocProperty(value="全拼音",required=true)
private String fullPinYin;
/**
* 简拼
*/
//@AutoDocProperty(value="简拼",required=true)
private String shortPinYin;
/**
* 国家code
*/
//@AutoDocProperty(value="国家code",required=true)
private String countryCode;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getProvinceId() {
return provinceId;
}
public void setProvinceId(Integer provinceId) {
this.provinceId = provinceId;
}
public String getProvinceName() {
return provinceName;
}
public void setProvinceName(String provinceName) {
this.provinceName = provinceName;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getIsHot() {
return isHot;
}
public void setIsHot(Integer isHot) {
this.isHot = isHot;
}
public String getFirstChar() {
return firstChar;
}
public void setFirstChar(String firstChar) {
this.firstChar = firstChar;
}
public String getFullPinYin() {
return fullPinYin;
}
public void setFullPinYin(String fullPinYin) {
this.fullPinYin = fullPinYin;
}
public String getShortPinYin() {
return shortPinYin;
}
public void setShortPinYin(String shortPinYin) {
this.shortPinYin = shortPinYin;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
}