JAVA中fastjson与jackson两种json工具包对于json字符串与map、对象、集合之间的转换

首先先区分一下

//这两种其实本质上都是新定义一个map集合使用键值对的格式,不同的是<>内为指定的map中键值对的类型
       Map map = new HashMap();
        map.put(1,"abc");
        map.put("abc",1);
        //以上两种都可以塞入map中
        //<>中定义了map中键值对的类型,那么只能塞入相对应的类型
        Map<String,Object> map2 = new HashMap<String, Object>();
        map2.put("abc",1);
        map2.put(1,"abc");  //此时就报错,无法塞入此类型的键值对

使用fastjson

首先在pom.xml文件中导入maven依赖

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

json字符串转map集合

            //json字符串转为map集合
            String jsonStr1 = "{\"name\":\"小张\",\"age\":18,\"sex\":\"男\"}";
            Map map2 = JSON.parseObject(jsonStr1);
            System.out.println(map2);
            System.out.println(map2.get("name"));

map集合转json字符串

        //map转json字符串
            Map<String,Object> map3 = new HashMap<String, Object>();
            map3.put("学号",123456);
            map3.put("班级","幼儿班");
            map3.put("学校","翻斗幼儿园");
            String jsonStr2 = JSON.toJSONString(map3);
        jsonStr2="我是字符串:"+jsonStr2;
        System.out.println(jsonStr2);

对象转json字符串

 Student student = new Student();
        student.setAge(18);
        student.setId(1);
        student.setName("张三");
        System.out.println("对象:"+student);
        String jsonStr3 = JSON.toJSONString(student);
        System.out.println("对象转json字符串:"+jsonStr3);

json字符串转对象

 Student student1 =JSON.parseObject(jsonStr3,Student.class);
        System.out.println("json字符串转对象:"+student1);
        System.out.println(student1.getName());

集合转json字符串

 Student student2 =new Student();
        student2.setName("李四");
        student2.setId(2);
        student2.setAge(20);
        Student student3 =new Student();
        student3.setName("王五");
        student3.setAge(22);
        student3.setId(3);
        List<Student> studentList = new ArrayList<>();
        studentList.add(student);
        studentList.add(student2);
        studentList.add(student3);
        String jsonStr4 =JSON.toJSONString(studentList);
        System.out.println("数组转json字符串:"+jsonStr4);

json字符串转集合

 JSONArray jsonArray = JSON.parseArray(jsonStr4);
        System.out.println("先将json字符串转为json数组:"+jsonArray);
        List<Student> studentList1 = (List<Student>)JSONArray.parseArray(jsonArray.toString(), Student.class);
        System.out.println("json数组转集合:"+studentList1);
        Student student4 = studentList1.get(1);
        System.out.println("获取集合内下标为1的数据的name值:"+student4.getName());

目前解析Json的工具包主要有,Gson,FastJson,Jackson。之前一直使用的fastjson因为方便,直到看了一篇文章才发现fastjson、jackson、gson之间的区别,综合来看,Jackson的性能较优,稳定性也比较高,而且spring-boot-starter-web默认会引入Jackson包。决定使用jackson。
这是那篇文章的链接:https://www.sohu.com/a/410305558_753508

使用jackson

首先在pom.xml中导入maven依赖,使用spring-boot-starter-web模块时,会默认引入Jackson包

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

使用jackson首先需要创建Jackson的核心对象 ObjectMapper
而且需要在类方法后抛出可能存在的异常不然使用的函数会报错

  public static void main(String args[]) throws JsonProcessingException {

map转为json字符串

                //map转为json字符串
               Map<Integer, Object> map = new HashMap<Integer, Object>();
                 map.put(1,"a");
                 map.put(2,"b");
                 map.put(3,"c");

            ObjectMapper mapper = new ObjectMapper();
               String jsonStr = mapper.writeValueAsString(map);
        System.out.println("map转为json字符串"+jsonStr);

json字符串转为map

        String jsonStr1 = "{\"name\":\"小张\",\"age\":18,\"sex\":\"男\"}";
        Map map1 = mapper.readValue(jsonStr1,Map.class);
        System.out.println("字符串转为集合"+map1);
        System.out.println(map1.get("name"));

对象转json字符串

 //对象转json字符串
        //单个对象
        User user =new User();
        user.setName("张三");
        user.setAge(18);
      String jsonStr2= mapper.writeValueAsString(user);
        System.out.println("对象转json"+jsonStr2);

json字符串转对象

 //json字符串转对象
        User u = mapper.readValue(jsonStr2,User.class);
        System.out.println("打印U:"+u);
        System.out.println(u.getName());

集合转json字符串

        //复杂对象集合
        User user1 = new User();
        user1.setName("李四");
        user1.setAge(20);
        User user2 = new User();
        user2.setName("王五");
        user2.setAge(22);
        List<User> userList = new ArrayList<>();
        userList.add(user);
        userList.add(user1);
        userList.add(user2);
        String nameTest = userList.get(1).getName();
        System.out.println("测试能否打印"+nameTest);
        String jsonStr3 = mapper.writeValueAsString(userList);
        System.out.println("集合对象转json"+jsonStr3);

json字符串转集合
json字符串如果要转集合等复杂情况,需要自定义输出类型

        JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, User.class);   //json字符串如果要转集合等复杂情况,需要自定义输出类型

        List<User> userList1 = mapper.readValue(jsonStr3,javaType);
        System.out.println("json字符串转数组对象"+userList1);
        System.out.println("打印集合中下标为1的数据"+userList1.get(1));
        User user3 = userList1.get(1);
        System.out.println("测试打印对象"+user3);
        System.out.println(user3.getName());
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值