当前位置:   article > 正文

JSONObject详解(com.alibaba)-fastjson2_com.alibaba.fastjson2

com.alibaba.fastjson2

前言

fastjson2 是 FASTJSON 项目的重要升级,目标是为下一个十年提供一个高性能的JSON库, fastjson2 性能相比原先旧的 fastjson 有了很大提升,并且 fastjson2 更安全,完全删除autoType白名单,提升了安全性。

关于fastjson2升级指南

fastjson2 的github地址

一、导入fastjson2依赖

maven如下:

pom(本文所有代码仅使用这一个依赖即可):

  

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.alibaba.fastjson2</groupId>
  4. <artifactId>fastjson2</artifactId>
  5. <version>2.0.26</version>
  6. </dependency>
  7. </dependencies>

需要注意的一点是在使用 fastjson2 时导入的包名是 com.alibaba.fastjson2 ,就像下面这样:

import com.alibaba.fastjson2.JSON;

import com.alibaba.fastjson2.JSONObject;

import com.alibaba.fastjson2.JSONArray;

二、json对象与json数组的创建

json对象创建

  1. import com.alibaba.fastjson2.JSONObject;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. JSONObject info = new JSONObject();
  5. info.put("name", "张三");
  6. info.put("age", "18");
  7. info.put("地理", "70");
  8. info.put("英语", "60");
  9. System.out.println(info);
  10. }
  11. }

json数组创建

  1. import com.alibaba.fastjson2.JSONArray;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. JSONArray array = new JSONArray();
  5. array.add("1班");
  6. array.add("2班");
  7. array.add("3班");
  8. System.out.println(array);
  9. }
  10. }

  1. import com.alibaba.fastjson2.JSONArray;
  2. import com.alibaba.fastjson2.JSONObject;
  3. public class Demo {
  4. public static void main(String[] args) {
  5. JSONObject info1 = new JSONObject();
  6. info1.put("name", "张三");
  7. info1.put("age", "18");
  8. JSONObject info2 = new JSONObject();
  9. info2.put("name", "李四");
  10. info2.put("age", "19");
  11. //把上面创建的两个json对象加入到json数组里
  12. JSONArray array = new JSONArray();
  13. array.add(info1);
  14. array.add(info2);
  15. System.out.println(array);
  16. }
  17. }

三、json对象取值与json数组遍历取值

json对象取值

  1. import com.alibaba.fastjson2.JSONArray;
  2. import com.alibaba.fastjson2.JSONObject;
  3. public class Demo {
  4. public static void main(String[] args) {
  5. JSONArray array = new JSONArray();
  6. array.add("1班");
  7. array.add("2班");
  8. array.add("3班");
  9. JSONObject school = new JSONObject();
  10. school.put("schoolName", "第一中学");
  11. school.put("teacher", "刘梅");
  12. JSONObject info = new JSONObject();
  13. info.put("name", "张三");
  14. info.put("age", "18");
  15. info.put("gradle", array);
  16. info.put("schoolInfo", school);
  17. //从info中取值
  18. System.out.println(info.getString("name")); //张三
  19. System.out.println(info.getIntValue("age"));//18
  20. System.out.println(info.getJSONArray("gradle"));//["1班","2班","3班"]
  21. System.out.println(info.getJSONObject("schoolInfo"));//{"schoolName":"第一中学","teacher":"刘梅"}
  22. }
  23. }

json数组遍历取值

  1. import com.alibaba.fastjson2.JSONArray;
  2. import com.alibaba.fastjson2.JSONObject;
  3. public class Demo {
  4. public static void main(String[] args) {
  5. JSONObject info1 = new JSONObject();
  6. info1.put("name", "张三");
  7. info1.put("age", "18");
  8. JSONObject info2 = new JSONObject();
  9. info2.put("name", "李四");
  10. info2.put("age", "19");
  11. JSONArray array = new JSONArray();
  12. array.add(info1);
  13. array.add(info2);
  14. //遍历获取json数组中对象的值
  15. for (int i = 0; i < array.size(); i++) {
  16. JSONObject json = array.getJSONObject(i);
  17. System.out.println(json.getString("name"));
  18. System.out.println(json.getString("age"));
  19. }
  20. }
  21. }

四、json对象与字符串的转换

json对象与字符串的转换

  1. import com.alibaba.fastjson2.JSON;
  2. import com.alibaba.fastjson2.JSONObject;
  3. public class Demo {
  4. public static void main(String[] args) {
  5. JSONObject info = new JSONObject();
  6. info.put("name", "张三");
  7. info.put("age", "18");
  8. info.put("地理", "70");
  9. info.put("英语", "60");
  10. //JSON对象转字符串
  11. String str = JSON.toJSONString(info);
  12. System.out.println(str);
  13. //JSON字符串转JSON对象
  14. JSONObject json = JSONObject.parseObject(str);
  15. System.out.println(json);
  16. }
  17. }

json字符串的字节数组转json对象

  1. import com.alibaba.fastjson2.JSON;
  2. import com.alibaba.fastjson2.JSONObject;
  3. public class Demo {
  4. public static void main(String[] args) {
  5. String str = "{\"name\":\"张三\",\"age\":\"18\",\"地理\":\"70\",\"英语\":\"60\"}";
  6. byte[] bytes = str.getBytes();
  7. JSONObject data = JSON.parseObject(bytes);
  8. System.out.println(data);
  9. }
  10. }

五、json数组与字符串的转换

  1. import com.alibaba.fastjson2.JSON;
  2. import com.alibaba.fastjson2.JSONArray;
  3. public class Demo {
  4. public static void main(String[] args) {
  5. String text = "[\"张三\",\"李四\",\"王五\"]";
  6. System.out.println(text);
  7. //json字符串转json数组
  8. JSONArray data = JSON.parseArray(text);
  9. //json数组转json字符串
  10. String str = JSONArray.toJSONString(data);
  11. System.out.println(data);
  12. System.out.println(str);
  13. }
  14. }

六、json字符串转java对象的转换

Student类如下:

  1. public class Student {
  2. public String name;
  3. public int age;
  4. public String getName() {
  5. return name;
  6. }
  7. public void setName(String name) {
  8. this.name = name;
  9. }
  10. public int getAge() {
  11. return age;
  12. }
  13. public void setAge(int age) {
  14. this.age = age;
  15. }
  16. public Student(String name, int age) {
  17. this.name = name;
  18. this.age = age;
  19. }
  20. }

json字符串转java对象的转换

  1. import com.alibaba.fastjson2.JSON;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. Student student = new Student("张三", 18);
  5. //Student对象转JSON字符串
  6. String studentStr = JSON.toJSONString(student);
  7. //JSON字符串转Student对象
  8. Student data = JSON.parseObject(studentStr, Student.class);
  9. System.out.println(studentStr);
  10. }
  11. }

java对象转byte数组转换

  1. import com.alibaba.fastjson2.JSON;
  2. public class Demo {
  3. public static void main(String[] args) {
  4. Student student = new Student("张三", 18);
  5. //Student对象转字节数组
  6. byte[] text = JSON.toJSONBytes(student);
  7. //字节数组转Student对象
  8. Student data = JSON.parseObject(text, Student.class);
  9. System.out.println(data.getName() + data.getAge());
  10. }
  11. }

七、json字符串与Map转换

json字符串转Map

  1. import com.alibaba.fastjson2.JSONObject;
  2. import com.alibaba.fastjson2.TypeReference;
  3. import java.util.Map;
  4. public class Demo {
  5. public static void main(String[] args) {
  6. String str = "{\n" +
  7. "\"gradle\":\"高一\",\n" +
  8. "\"number\":\"2\",\n" +
  9. "\"people\":[{\"name\":\"张三\",\"age\":\"15\",\"phone\":\"123456\"},\n" +
  10. " {\"name\":\"李四\",\"age\":\"16\",\"phone\":\"78945\"}]\n" +
  11. "}";
  12. //json字符串转Map
  13. Map<String, Object> map = JSONObject.parseObject(str, new TypeReference<Map<String, Object>>() {
  14. });
  15. System.out.println(map.get("gradle").toString());
  16. System.out.println(map.get("number").toString());
  17. System.out.println(map.get("people").toString());
  18. }
  19. }

Map转json字符串

(注意:如果直接使用JSON.toJSONString(map)转换,因为"测试1"的值为null,转换的结果就会是 {“测试2”:“hello”})

  1. import com.alibaba.fastjson2.JSON;
  2. import com.alibaba.fastjson2.JSONWriter;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. public class Demo {
  6. public static void main(String[] args) {
  7. Map<String, String> map = new HashMap<>();
  8. map.put("测试1", null);
  9. map.put("测试2", "hello");
  10. //{"测试2":"hello","测试1":null}
  11. String str = JSON.toJSONString(map, JSONWriter.Feature.WriteMapNullValue);
  12. System.out.println(str);
  13. }
  14. }

如果你使用的是老的fastjson1(下述是简单示例,不可使用),可以像下面这样转换:

     

  1. Map<String, String> map = new HashMap<>();
  2. map.put("测试1", null);
  3. map.put("测试2", "hello");
  4. String str = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue) ;

八、json数组转List

  1. import com.alibaba.fastjson2.JSONArray;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import java.util.List;
  4. import java.util.Map;
  5. public class Demo {
  6. public static void main(String[] args) {
  7. String str = "{\n" +
  8. "\"gradle\":\"高一\",\n" +
  9. "\"number\":\"2\",\n" +
  10. "\"people\":[{\"name\":\"张三\",\"age\":\"15\",\"phone\":\"123456\"},\n" +
  11. " {\"name\":\"李四\",\"age\":\"16\",\"phone\":\"78945\"}]\n" +
  12. "}";
  13. JSONObject strJson = JSONObject.parseObject(str);
  14. //获取people数组
  15. JSONArray people = strJson.getJSONArray("people");
  16. //json数组转List
  17. List<Map> list = people.toJavaList(Map.class);
  18. System.out.println(str);
  19. }
  20. }

如果你使用的是老的fastjson1,可以像下面这样转换:       

  1. String str = "{\n" +
  2.                 "\"gradle\":\"高一\",\n" +
  3.                 "\"number\":\"2\",\n" +
  4.                 "\"people\":[{\"name\":\"张三\",\"age\":\"15\",\"phone\":\"123456\"},\n" +
  5.                 "         {\"name\":\"李四\",\"age\":\"16\",\"phone\":\"78945\"}]\n" +
  6.                 "}";
  7. JSONObject strJson = JSONObject.parseObject(str);//字符串转json对象
  8. String people = String.valueOf(strJson.getJSONArray("people"));
  9. List<Map<String, String>> list = (List<Map<String, String>>) JSONArray.parse(people);

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/532669
推荐阅读
相关标签
  

闽ICP备14008679号