JAVA数据类型转换

发布于:2023-01-08 ⋅ 阅读:(451) ⋅ 点赞:(0)

Json字符串转换

jar包:

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.83</version>
        </dependency>

Json

json转map

	String jsonStr = "json字符串";
    HashMap hashMap = JSON.parseObject("jsonStr", HashMap.class);

json转list

//实体类对象: FullScoreTeacherVO
//注意: json转List不能用'fullScoreObj.toString()',而是得用 JSONArray.toJSONString(fullScoreObj)

Object fullScoreObj = map.get("fullScore");
List<FullScoreTeacherVO> fullScoreList = JSONObject.parseArray(JSONArray.toJSONString(fullScoreObj), FullScoreTeacherVO.class);

json转 对象

//实体类对象: ExamInfoTeacherVO
//注意: json转String不能用'examInfoObj.toString()',而是得用 JSONObject.toJSONString(JSON.parse(examInfoObj)
Object examInfoObj = map.get("examInfo");
ExamInfoTeacherVO examInfo = JSONObject.parseObject(JSONObject.toJSONString(JSON.parse(examInfoObj)), ExamInfoTeacherVO.class);

Map

map转json

JSONObject json = new JSONObject(map);

map转 obj

// 将 Map 转换为 实体类
User user = JSON.parseObject(JSON.toJSONString(map), User.class);

List

list转json

String str = JSON.toJSONString(userList);

Obj

对象转json

 //暂无

obj转Map


HashMap hashMap = JSON.parseObject(JSON.toJSONString(object), HashMap.class);

HashMap resultMap = JSON.parseObject(object.toString(), HashMap.class);

obj转List

List<String> list = JSON.parseObject(JSON.toJSONString(listObj),List.class) ;

String

String 转 JSON

	JSONObject jsonObject = JSONObject.parseObject(jsonStr);
本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到