json串与各种类型对象互转

一、对象 转 json串

1、实体 类 转 json串:String beanToJson = JSONObject.toJSONString(objectBean);

2、map 转 json串:String mapToJson = JSONObject.toJSONString(map);

3、list 转 json串:

                                List<String> list = new ArrayList<>();

                                list.add(JSONObject.toJSONString(map1));

                                list.add(JSONObject.toJSONString(map2));

                                String listToJson = list.toString();

二、json串转对象

1、json串 转 实体类:

                JSONObject jsonObject = JSONObject.parseObject(jsonStr);

                StudentBean jsonToBean = JSONObject.toJavaObject(jsonObject, StudentBean.class);

                转实体类集合

                String jsonStr = JSON.toJSONString(object);

                 List<VO> list = JSON.parseArray(jsonStr, VO.class);

2、 json串 转 Map:

HashMap<String, Object> jsonToMap = JSONObject.parseObject(mapToJson2, HashMap.class);

3、按json串元素顺序 转 Map:

HashMap<String, Object> jsonToLinkedMap = JSONObject.parseObject(mapToJson2, LinkedHashMap.class, Feature.OrderedField);

三、效果展示

1、建student实体类

public class StudentBean {
    private String stuName;
    private String stuAge;

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public String getStuAge() {
        return stuAge;
    }

    public void setStuAge(String stuAge) {
        this.stuAge = stuAge;
    }
}

2、转换示例

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @date 2022/1/25 10:53
 * @describe
 */
public class StudentService {
    public static void main(String[] args) {
        /****** 实体类 ******/
        StudentBean studentBean = new StudentBean();
        studentBean.setStuName("张三");
        studentBean.setStuAge("18");
        /****** map1 ******/
        Map<String, String> map1 = new HashMap<>();
        map1.put("stuAge", "李四");
        map1.put("stuName", "20");
        /****** map2 ******/
        Map<String, String> map2 = new HashMap<>();
        map2.put("stuAge", "魑魅");
        map2.put("stuName", "15");
        /****** map3 ******/
        Map<String, String> map3 = new HashMap<>();
        map3.put("stuAge", "魍魉");
        map3.put("stuName", "15");
        /****** list ******/
        List<String> list = new ArrayList<>();
        list.add(JSONObject.toJSONString(map2));
        list.add(JSONObject.toJSONString(map3));
        /****** map ******/
        Map<String, Object> stuMap = new HashMap<>();
        stuMap.put("stu1", studentBean);
        stuMap.put("stu2", map1);
       
        /****** 对象转 json串 开始  ******/
        
        /****** 实体类 转 json串 ******/
        String beanToJson = JSONObject.toJSONString(studentBean);
        System.out.println(beanToJson);
        // 输出:{"stuAge":"18","stuName":"张三"}
        /****** map 转 json串 ******/
        String mapToJson = JSONObject.toJSONString(map1);
        System.out.println(mapToJson);
        // 输出:{"stuName":"20","stuAge":"李四"}
        
        /****** map 转 json串 ******/
        String mapToJson1 = JSONObject.toJSONString(stuMap);
        System.out.println(mapToJson1);
        // 输出:{"stu2":{"stuName":"20","stuAge":"李四"},"stu1":{"stuAge":"18","stuName":"张三"}}
        
        /****** list 转 json串 ******/
        String listToJson = list.toString();
        System.out.println(listToJson);
        // 输出:[{"stuName":"15","stuAge":"魑魅"}, {"stuName":"15","stuAge":"魍魉"}]
        
        /****** map 转 json串 ******/
        stuMap.put("stu3", listToJson);
        String mapToJson2 = JSONObject.toJSONString(stuMap);
        System.out.println(mapToJson2);
        // 输出:{"stu2":{"stuName":"20","stuAge":"李四"},"stu3":"[{\"stuName\":\"15\",\"stuAge\":\"魑魅\"}, {\"stuName\":\"15\",\"stuAge\":\"魍魉\"}]","stu1":{"stuAge":"18","stuName":"张三"}}
        /****** 对象转 json串 结束  ******/

        /****** json串转 对象 开始  ******/
        /****** json串 转 实体类 ******/
        JSONObject jsonObject = JSONObject.parseObject(beanToJson);
        StudentBean jsonToBean = JSONObject.toJavaObject(jsonObject, StudentBean.class);

        /****** json串 转 Map ******/
        HashMap<String, Object> jsonToMap = JSONObject.parseObject(mapToJson2, HashMap.class);
        System.out.println(jsonToMap);
        // 输出:{stu2={"stuName":"20","stuAge":"李四"}, stu3=[{"stuName":"15","stuAge":"魑魅"}, {"stuName":"15","stuAge":"魍魉"}], stu1={"stuName":"张三","stuAge":"18"}}
        
        /****** 按json串元素顺序 转 Map ******/
        HashMap<String, Object> jsonToLinkedMap = JSONObject.parseObject(mapToJson2, LinkedHashMap.class, Feature.OrderedField);
        System.out.println(mapToJson2);
        // 输出:{"stu2":{"stuName":"20","stuAge":"李四"},"stu3":"[{\"stuName\":\"15\",\"stuAge\":\"魑魅\"}, {\"stuName\":\"15\",\"stuAge\":\"魍魉\"}]","stu1":{"stuAge":"18","stuName":"张三"}}
        /****** json串转 对象 结束  ******/
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值