com.alibaba.fastjson.JSONObject之对象与JSON转换方法
com.alibaba.fastjson.JSONObject时经常会用到它的转换方法,包括Java对象转成JSON串、JSON对象,JSON串转成java对象、JSON对象,JSON对象转换Java对象、JSON串等,使用方法总结如下,持续更新...
动动小手点个关注呗,右下方推荐走一波~
1、Java对象-->JSON对象
(JSONObject)JSONObject.toJSON(Java对象实例)
1 public class JSON2JavaTest{ 2 public static void main(String[] args) { 3 Student stu = new Student("公众号BiggerBoy", "m", 2); 4 5 //Java对象转化为JSON对象 6 JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu); 7 System.out.println("Java对象转化为JSON对象\n" + jsonObject);//{"name":"公众号编程大道","age":2,"sex":"m"} 8 9 } 10 }
2、Java对象-->JSON字符串
JSONObject.toJSONString(Java对象实例)
1 public class JSON2JavaTest{ 2 public static void main(String[] args) { 3 Student stu = new Student("公众号BiggerBoy", "m", 2); 4 5 //Java对象转换成JSON字符串 或者JSON.toJSONString(stu); JSONObject继承了JSON 6 String stuString = JSONObject.toJSONString(stu); 7 System.out.println("Java对象转换成JSON字符串\n" + stuString);//{"age":2,"name":"公众号编程大道","sex":"m"} 8 } 9 }
3、JSON对象-->JSON字符串
JSONObject.toJSONString();
1 public class JSON2JavaTest{ 2 public static void main(String[] args) { 3 Student stu = new Student("公众号BiggerBoy", "m", 2); 4 5 //先转成JSON对象 6 JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu); 7 8 //JSON对象转换为JSON字符串 9 String jsonString = jsonObject.toJSONString(); 10 System.out.println("JSON对象转换为JSON字符串\n" + jsonString);//{"name":"公众号编程大道","age":2,"sex":"m"} 11 12 } 13 }
4、JSON对象-->Java对象
JSONObject.toJavaObject(JSON对象实例, Java对象.class);
public class JSON2JavaTest{ public static void main(String[] args) { Student stu = new Student("公众号BiggerBoy", "m", 2); //先转成JSON对象 JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu); //JSON对象转换成Java对象 Student student = JSONObject.toJavaObject(jsonObject, Student.class); System.out.println("JSON对象转换成Java对象\n" + student);//Student{name='公众号编程大道', sex='m', age=2} } }
5、JSON字符串-->JSON对象
JSONObject.parseObject(JSON字符串)
1 public class JSON2JavaTest{ 2 public static void main(String[] args) { 3 String stuString = "{\"age\":2,\"name\":\"公众号 BiggerBoy\",\"sex\":\"m\"}"; 4 5 //JSON字符串转换成JSON对象 6 JSONObject jsonObject1 = JSONObject.parseObject(stuString); 7 System.out.println("JSON字符串转换成JSON对象\n" + jsonObject1);//{"sex":"m","name":"公众号编程大道","age":2} 8 } 9 }
6、JSON字符串-->Java对象
JSONObject.parseObject(JSON字符串, Java对象.class);
1 public class JSON2JavaTest{ 2 public static void main(String[] args) { 3 String stuString = "{\"age\":2,\"name\":\"公众号 BiggerBoy\",\"sex\":\"m\"}"; 4 5 //JSON字符串转换成Java对象 6 Student student1 = JSONObject.parseObject(stuString, Student.class); 7 System.out.println("JSON字符串转换成Java对象\n" + student1);//Student{name='公众号编程大道', sex='m', age=2} 8 } 9 }
如果student对象内有一个对象集合hobbies,相应的,JSON字符串转Map时如果不做处理,hobbies内的字段顺序可能会和原始hobbies字段值不一样
如下,第一次打印hobbies中的hobbyType跑到hobbyName前了,而原始JSON串中恰好和这相反,对于接口加解密生成签名时,字符串顺序尤为重要,若要保证原始顺序,可以加一个参数Feature.OrderedField
第二次打印的hobbies字段内的字符串顺序和原始字符串中一致。
String str1 = "{\"age\":2,\"hobbies\":[{\"hobbyName\":\"篮球\",\"hobbyType\":1},{\"hobbyName\":\"跳绳\",\"hobbyType\":2}],\"name\":\"公众号BiggerBoy\",\"sex\":\"m\"}"; Map map = JSONObject.parseObject(str1, Map.class); System.out.println(JSONObject.toJSONString(map));
//{"hobbies":[{"hobbyType":1,"hobbyName":"篮球"},{"hobbyType":2,"hobbyName":"跳绳"}],"sex":"m","name":"公众号BiggerBoy","age":2} Map map1 = JSONObject.parseObject(str1, Map.class, Feature.OrderedField); System.out.println(JSONObject.toJSONString(map1));
//{"hobbies":[{"hobbyName":"篮球","hobbyType":1},{"hobbyName":"跳绳","hobbyType":2}],"sex":"m","name":"公众号BiggerBoy","age":2}
7、JSON字符串-->List<Java对象>
List<T> parseArray(JSON字符串, Java对象.class)
public class JSON2JavaTest{ public static void main(String[] args) { String stuString = "[{\"age\":2,\"name\":\"公众号\",\"sex\":\"m\"},{\"age\":18,\"name\":\"BiggerBoy\",\"sex\":\"m\"}]"; //JSON字符串转换成Java对象 List<Student> studentList = JSONObject.parseArray(stuString, Student.class); System.out.println("JSON字符串转换成Java对象list\n" +studentList );
}
}
热门文章
一个属性同时使用Autowired和Resource注解会发生什么?
说的再好,不如行动。不怕慢,就怕站。