JSONObject,JSONArray与java实体类相互转换
正常情况下:
-
JSONObject转换为实体类: Employee employee = JSONObject.toJavaObject(param, Employee.class);
-
JSONArray转换为实体类数组: List employees = JSON.parseArray(jsonArray.toJSONString(), Employee.class);
-
实体类转JSONObject: JSONObject jsonObject = JSONObject.parseObject(employee.toString());
-
实体类数组转JSONArray: JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(employees));
当JSONOBject的键值与实体类不一直的情况下,可以重新构造JSONObject,但只限于实体类字段比较少的情况下,太多则就不适合,那么我是利用实体类的构造方法来构造实体的:直接上代码吧!
public class CaseOrder {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
private String dateTime; //日期
private String provinceName; //省份名称
private String businessArea; //业务领域
private String content; //工单内容
private String type; //工单类型
private String insertTime; //入库时间
private ArrayList<CaseOrder> caseOrderList = new ArrayList<>(); //实体类数组当作容器使用,盛装所有构造出来的实体
private static final HashMap<String,String> map = new HashMap();
//静态代码块构造反射用的map;
static {
map.put("provinceName","省份名称");
map.put("businessArea","业务领域");
map.put("content","工单内容");
map.put("type","工单类型");
}
public CaseOrder() {
}
/**
* 传入JSONObject的数据
* {"time": "2020-01-11 13:15:10",
* "data":
* [
* {"省份名称": "北京","业务领域": "城市客运","工单内容": "投诉出租车甩客","工单类型": "投诉举报"},
* {"省份名称": "北京","业务领域": "高速公路","工单内容": "投诉ETC扣费异常","工单类型": "投诉举报"}
* ]
* }
* @param jsonObject1
*/
public CaseOrder(JSONObject jsonObject1){
JSONArray data = jsonObject1.getJSONArray("data");
//遍历data,得到所有Object
for (int i = 0; i < data.size(); i++) {
//得到jsonObject1,判断是否有key值未为省份名称,存储省份名称,删除省份名称key值,
JSONObject jsonObject2 = data.getJSONObject(i);
//利用反射创建CaseOrder的class文件
Class<CaseOrder> caseOrderClass = CaseOrder.class;
//定义实体类
CaseOrder caseOrder = null;
try {
//反射初始化实体类
caseOrder = caseOrderClass.newInstance();
//得到所有实体类字段
Field[] declaredFields = caseOrderClass.getDeclaredFields();
for (Field field : declaredFields) {
//打开暴力反射
field.setAccessible(true);
//得到字段名称
String name = field.getName();
//利用switch case default结合map ,去设置每个字段对应的值
switch (name) {
case "dateTime":
field.set(caseOrder, jsonObject1.getString("time"));
break;
case "caseOrderList":
break;
case "map":
break;
case "df":
break;
case "insertTime":
field.set(caseOrder, df.format(new Date()));
break;
default:
field.set(caseOrder, jsonObject2.getString(map.get(name)));
break;
}
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
//最后将实体添加到数组当中
caseOrderList.add(caseOrder);
}
}
//设置实体类数组的get方法
public ArrayList<CaseOrder> getCaseOrderList() {
return caseOrderList;
}
@Override
public String toString() {
return "CaseOrder{" +
"dateTime='" + dateTime + '\'' +
", provinceName='" + provinceName + '\'' +
", businessArea='" + businessArea + '\'' +
", content='" + content + '\'' +
", type='" + type + '\'' +
", insertTime='" + insertTime + '\'' +
'}';
}
}
测试方法
public class test {
public static void main(String[] args) {
String str = "{\n" +
"\t\"time\": \"2020-01-11 13:15:10\",\n" +
"\t\"data\": [{\n" +
"\t\t\"省份名称\": \"北京\",\n" +
"\t\t\"业务领域\": \"城市客运\",\n" +
"\t\t\"工单内容\": \"投诉出租车甩客\",\n" +
"\t\t\"工单类型\": \"投诉举报\"\n" +
"\t},{\n" +
"\t\t\"省份名称\": \"北京\",\n" +
"\t\t\"业务领域\": \"高速公路\",\n" +
"\t\t\"工单内容\": \"投诉ETC扣费异常\",\n" +
"\t\t\"工单类型\": \"投诉举报\"\n" +
"\t}]\n" +
"}\n";
System.out.println(str);
JSONObject jsonObject = JSONObject.parseObject(str);
CaseOrder caseOrder = new CaseOrder(jsonObject);
System.out.println(caseOrder.getCaseOrderList());
结果打印:
[
CaseOrder
{dateTime=‘2020-01-11 13:15:10’,
provinceName=‘北京’,
businessArea=‘城市客运’,
content=‘投诉出租车甩客’,
type=‘投诉举报’,
insertTime='2020-01-12 12:14:18
'},
CaseOrder
{dateTime=‘2020-01-11 13:15:10’,
provinceName=‘北京’,
businessArea=‘高速公路’,
content=‘投诉ETC扣费异常’,
type=‘投诉举报’,
insertTime=‘2020-01-12 12:14:18’
}
]
如果有更好的方法,请不吝赐教!