赞
踩
JSON.toJSONString() // 将java对象、java集合、Json对象转为jsonString
JSON.toJSON() // 将java对象、java集合转为json对象
JSON.parse() // 将jsonString解析为jsonObject或者jsonArray
JSON.parseObject() // 将jsonString解析为javaObject或者jsonObject
JSON.parseArray() // 将jsonString解析为javaObjectList或者jsonArray
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-scala_3</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.38</version>
</dependency>
package com.example.student20230710.domain; public class Student { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "id=" + id + ", name=" + name + ", age=" + age + '}'; } }
package com.example.student20230710.test; import com.alibaba.fastjson2.JSON; import com.example.student20230710.domain.Student; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class fastjson { public static void main(String[] args) throws SQLException, JsonProcessingException { // java转JSON // JSON.toJSONString() // 将java对象、java集合、Json对象转为jsonString // JSON.toJSON() // 将java对象、java集合转为json对象 // JSON转Java // JSON.parse() // 将jsonString解析为jsonObject或者jsonArray // JSON.parseObject() // 将jsonString解析为javaObject或者jsonObject // JSON.parseArray() // 将jsonString解析为javaObjectList或者jsonArray Student yyhstudent = new Student(); yyhstudent.setId(1); yyhstudent.setName("yyh"); yyhstudent.setAge(24); Student dystudent = new Student(); dystudent.setId(2); dystudent.setName("dy"); dystudent.setAge(24); List<Student> list = new ArrayList<Student>(); list.add(yyhstudent); list.add(dystudent); //对象转json,会存在顺序错了的问题(用的fastjson) String studentJson = JSON.toJSONString(yyhstudent); System.out.println("1------对象转json串:" +studentJson);//{"age":24,"id":1,"name":"yyh"} 顺序发生了变化 //对象转json,不会存在顺序错了的问题(用的jackson) String studentJson1 = new ObjectMapper().writeValueAsString(yyhstudent); System.out.println("2------对象转json串:" +studentJson1); //json转对象(用的fastjson) Student student1 = JSON.parseObject(studentJson,Student.class); System.out.println("3------json转对象:" +student1.toString()); // 集合转json(会出现单个对象顺序问题)(用的fastjson) String listJson =JSON.toJSONString(list); System.out.println("集合转json串:" + listJson); // 集合转json(不会出现单个对象顺序问题)(用的jackson) String listJson2 = new ObjectMapper().writeValueAsString(list);//对象转json,不会存在顺序错了的问题 System.out.println("集合转json串:" + listJson2); // json字符串转List<Object>对象(用的fastjson) List<Student> studentList11 = JSON.parseArray(listJson2, Student.class); System.out.println("json字符串转List<Object>对象:"+studentList11.toString()); // <dependency> // <groupId>com.fasterxml.jackson.module</groupId> // <artifactId>jackson-module-scala_3</artifactId> // <version>2.15.2</version> // </dependency> // // <dependency> // <groupId>com.alibaba.fastjson2</groupId> // <artifactId>fastjson2</artifactId> // <version>2.0.38</version> // </dependency> } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。