电商网站入口网站seo排名优化
最近,工作中会涉及到Java对象与JSON字符串相互转换,虽然说并不难,但打算还是梳理一番,主要内容有:
JSON 字符串 转 普通对象
普通对象 转 JSON 字符串
JSON 字符串数组 转 List 集合对象
List 集合对象 转 JSON 字符串数组
JSON 字符串 转 装有对象的 Map
装有对象的Map 转 JSON 字符串
开始之前,需要在 pom.xml 引入相关的 jar 包:
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.16.10</version><scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.75</version>
</dependency>
创建一个普通对象,使用 lombok 提供的注解:
@Data
public class ExtInfo {private String orderId;private String creatTime;private String postion;private String orderType;private String amount;
}
测试1:json字符串 与 普通对象 互转
public class Demo {@Testvoid test1() {// json字符串转普通对象String jsonStr = "{\"orderId\":\"1111\",\"creatTime\":\"20210817223001\",\"postion\":\"hangZhou xiXi\",\"orderType\":\"4\",\"amount\":\"20\"}";JSONObject jsonObject = JSONObject.parseObject(jsonStr);ExtInfo extInfo = JSONObject.toJavaObject(jsonObject, ExtInfo.class);System.out.println(extInfo);System.out.println(extInfo.getOrderId());// 普通对象转json字符串String str = JSONObject.toJSONString(extInfo);System.out.println(str);}
}
测试结果:
ExtInfo(orderId=1111, creatTime=20210817223001, postion=hangZhou xiXi, orderType=4, amount=20)
1111
{"amount":"20","creatTime":"20210817223001","orderId":"1111","orderType":"4","postion":"hangZhou xiXi"}
测试2:json字符串数组 与 List集合对象 互转
public class Demo2 {@Testvoid test2() { // json字符串数组转对象List集合String jsonStrArray = "[{\"orderId\":\"1111\",\"creatTime\":\"20210817223001\",\"postion\":\"hangZhou xiXi\",\"orderType\":\"4\",\"amount\":\"20\"}]";JSONArray jsonArray = JSONObject.parseArray(jsonStrArray);// 方式一、根据索引获取 JSONArray 中的对象ExtInfo extInfo1 = jsonArray.getObject(0, ExtInfo.class);System.out.println(extInfo1);// 方式二、转成List,再从List获取对象List<ExtInfo> extInfos = jsonArray.toJavaList(ExtInfo.class);ExtInfo extInfo2 = extInfos.get(0);System.out.println(extInfo2);// List集合对象转json字符串数组String strs = JSONObject.toJSONString(extInfos);System.out.println(strs);}
}
测试结果
ExtInfo(orderId=1111, creatTime=20210817223001, postion=hangZhou xiXi, orderType=4, amount=20)
ExtInfo(orderId=1111, creatTime=20210817223001, postion=hangZhou xiXi, orderType=4, amount=20)
[{"amount":"20","creatTime":"20210817223001","orderId":"1111","orderType":"4","postion":"hangZhou xiXi"}]
测试3:json字符串 与 装有对象的Map 互转
public class Demo3 {@Testvoid test3() { // map格式转对象String mapJsonStr = "{\"level\":{\"orderId\":\"1111\",\"creatTime\":\"20210817223001\",\"postion\":\"hangZhou xiXi\",\"orderType\":\"4\",\"amount\":\"20\"}}";Map<String, ExtInfo> extInfoMap = JSONObject.parseObject(mapJsonStr,new TypeReference<HashMap<String, ExtInfo>>() {});System.out.println(extInfoMap);System.out.println(extInfoMap.get("level"));// 对象转map格式String mapStr = JSONObject.toJSONString(extInfoMap);System.out.println(mapStr);}
}
测试结果:
{level=ExtInfo(orderId=1111, creatTime=20210817223001, postion=hangZhou xiXi, orderType=4, amount=20)}
ExtInfo(orderId=1111, creatTime=20210817223001, postion=hangZhou xiXi, orderType=4, amount=20)
{"level":{"amount":"20","creatTime":"20210817223001","orderId":"1111","orderType":"4","postion":"hangZhou xiXi"}}