Java对象与JSON字符串的互转

  1. JSON 字符串 转 普通对象
  2. 普通对象 转 JSON 字符串
  3. JSON 字符串数组 转 List 集合对象
  4. List 集合对象 转 JSON 字符串数组
  5. JSON 字符串 转 装有对象的 Map
  6. 装有对象的Map 转 JSON 字符串

最近,工作中会涉及到Java对象与JSON字符串相互转换,虽然说并不难,但打算还是梳理一番,主要内容有:

JSON 字符串 转 普通对象
普通对象 转 JSON 字符串
JSON 字符串数组 转 List 集合对象
List 集合对象 转 JSON 字符串数组
JSON 字符串 转 装有对象的 Map
装有对象的Map 转 JSON 字符串
开始之前,需要在 pom.xml 引入相关的 jar 包:

org.projectlombok lombok 1.16.10 provided com.alibaba fastjson 1.2.75 接着,创建一个普通对象,使用 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 {
@Test
void 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 {
@Test
void 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 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 {
@Test
void 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”}}
文章知识点与官方知识档案匹配,可进一步学习相关知识
————————————————
版权声明:本文为CSDN博主「谁是谁的小确幸」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_29119581/article/details/126415705

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值