FastJson使用和案例
FastJson使用和案例
导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
使用案例
1.新建Perosn类,辅助测试
package com.study.rabbit.mqtest;
import lombok.Data;
import java.util.List;
/**
* @author LiFuhang
* @date 2024年03月10日 10:21
*/
@Data
public class Person {
private String name;
private String age;
private String skin;
private List<Person> childrenList;
}
2.新建测试类FastJsonTests,编写使用案例
package com.study.rabbit;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.rabbit.mqtest.Person;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
class FastJsonTests {
/**
* 消费者接受消息测试
*/
@Test
public void fastJsonStudy() {
Map<String, Object> mapMsg = new HashMap<>();
mapMsg.put("name", "张三");
mapMsg.put("age", "23");
//将map转为json
JSONObject jsonObject = new JSONObject(mapMsg);
System.out.println("map转为json : " + jsonObject);
//将json转为String
String strMsg = jsonObject.toJSONString();
System.out.println("json转为String : " + strMsg);
//将String转json
JSONObject jsonObject1 = JSONObject.parseObject(strMsg);
System.out.println("String转json : " + jsonObject1);
//将json转为实体类
Person person = jsonObject.toJavaObject(Person.class);
System.out.println("json转为实体类 : " + person);
//将json转为map
Map<String, Object> map = JSON.parseObject(jsonObject.toJSONString(), Map.class);
System.out.println("json转为map : " + map);
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
Person c1 = new Person();
c1.setName("儿子");
c1.setAge("19");
Person c2 = new Person();
c2.setName("闺女");
c2.setAge("21");
List<Person> cList = new ArrayList<>();
cList.add(c1);
cList.add(c2);
Person p = new Person();
p.setName("老子");
p.setAge("47");
p.setChildrenList(cList);
System.out.println("包含集合的实体类 :" + p);
//包含集合的实体类 转 json
JSONObject jsonP = (JSONObject) JSONObject.toJSON(p);
System.out.println("包含集合的实体类 转 json:" + jsonP);
//包含数组的json 转 对应的实体类
Person person1 = jsonP.toJavaObject(Person.class);
System.out.println("包含数组的json 转 对应的实体类 : " + person1);
//解析json内包含的数组
JSONArray jsonArray = jsonP.getJSONArray("childrenList");
System.out.println("解析json内包含的数组 :" + jsonArray);
//获取jsonArray中的第一条数据
JSONObject jsonObject2 = (JSONObject) jsonArray.get(0);
System.out.println("获取jsonArray中的第一条数据 : " + jsonObject2);
//字符串转JSONArray
JSONArray jsonArray1 = JSONObject.parseArray(jsonP.get("childrenList").toString());
System.out.println("字符串转成的jsonArray : " + jsonArray1);
//遍历 JSONArray
for (Object obj : jsonArray1){
//将json数组的每一条变为JSONObject
JSONObject infoObj = JSONObject.parseObject(JSONObject.toJSONString(obj));
//获取JSONObject中指定字段的值
String name = infoObj.get("name").toString();
String age = infoObj.get("age").toString();
System.out.println(name+"---"+age);
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律