• Alibaba Fastjson的基本使用


    目录

    1. 基本说明

    2. 引入依赖

    3. 其他类型转Json对象

    3.1 json字符串转JsonOject

    3.2 json数组字符串转JsonArray

    3.3 JavaBean 转 JsonObject

    3.4 JavaBean List转JsonArray

    4. 其他类型转Json字符串

    4.1 将Map转Json字符串

    4.2 将Java Bean 转Json字符串 

    4.3 将JavaBean List转Json字符串

    1. 基本说明

    FastJson 与 Google 的 Gson 都是解析 Json 的强者

    github地址: GitHub - alibaba/fastjson: FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.

    2. 引入依赖

    1. <dependency>
    2. <groupId>com.alibabagroupId>
    3. <artifactId>fastjsonartifactId>
    4. <version>1.2.49version>
    5. dependency>

    3. 其他类型转Json对象

    3.1 json字符串转JsonOject

    1. /**
    2. * json字符串转JsonOject
    3. */
    4. public static void test1() {
    5. String jsonStr = "{\"name\":\"sanqian\",\"age\":10,\"address\": \"shen zhen\"}";
    6. JSONObject jsonObject = JSONObject.parseObject(jsonStr);
    7. System.out.println(jsonObject.getString("name"));//输出one
    8. System.out.println(jsonObject.getInteger("age"));//输出110
    9. System.out.println(jsonObject.getString("address"));//输出null
    10. }

    3.2 json数组字符串转JsonArray

    1. /**
    2. * json数组字符串转JsonArray
    3. */
    4. public static void test2(){
    5. String jsonArrStr = "[{\"name\":\"张三\",\"age\":25},{\"name\":\"李四\",\"age\":28}]";
    6. JSONArray jsonArray = JSONObject.parseArray(jsonArrStr);
    7. for(Object object: jsonArray){
    8. JSONObject jsonObject = (JSONObject) object;
    9. System.out.println(jsonObject.get("name"));
    10. System.out.println(jsonObject.get("age"));
    11. System.out.println("--------------------");
    12. }
    13. }

    3.3 JavaBean 转 JsonObject

    1. /**
    2. * JavaBean 转 JsonObject
    3. */
    4. public static void test3(){
    5. Person person1 = new Person();
    6. person1.setName("张三");
    7. person1.setAge(28);
    8. person1.setBirthday(new Date());
    9. // 方式一:
    10. JSONObject jsonObject = (JSONObject) JSONObject.toJSON(person1);
    11. System.out.println(jsonObject);
    12. System.out.println(jsonObject.get("name"));
    13. //方式二
    14. String jsonString = JSONObject.toJSONString(person1);
    15. JSONObject jsonObject1 = JSONObject.parseObject(jsonString);
    16. System.out.println(jsonObject1);
    17. }

    3.4 JavaBean List转JsonArray

    1. /**
    2. * JavaBean List转JsonArray
    3. */
    4. public static void test4(){
    5. Person person1 = new Person();
    6. person1.setName("张三");
    7. person1.setAge(28);
    8. person1.setBirthday(new Date());
    9. Person person2 = new Person();
    10. person2.setName("李四");
    11. person2.setAge(25);
    12. person2.setBirthday(new Date());
    13. List persons = new ArrayList();
    14. persons.add(person1);
    15. persons.add(person2);
    16. /**方式1*/
    17. String jsonArrStr = JSONArray.toJSONString(persons);
    18. JSONArray jsonArray = JSONArray.parseArray(jsonArrStr);
    19. JSONObject jsonObject1 = (JSONObject)jsonArray.get(0);
    20. System.out.println(jsonObject1.get("name"));//输出:张三
    21. /**方式2*/
    22. JSONArray jsonArray1 = (JSONArray)JSONArray.toJSON(persons);
    23. JSONObject jsonObject2 = (JSONObject)jsonArray1.get(1);
    24. System.out.println(jsonObject2.get("name"));//输出:李四
    25. }

    4. 其他类型转Json字符串

    4.1 将Map转Json字符串

    1. /**
    2. * 将Map转Json字符串
    3. */
    4. public void test1() {
    5. Map map = new HashMap();
    6. map.put("key1", "One");
    7. map.put("key2", "Two");
    8. String mapJson = JSON.toJSONString(map);
    9. System.out.println(mapJson);//输出:{"key1":"One","key2":"Two"}
    10. }

    4.2 将Java Bean 转Json字符串 

    1. /**
    2. * 将Java Bean 转Json字符串
    3. */
    4. public static void test2() {
    5. Person person1 = new Person();
    6. person1.setName("张三");
    7. person1.setAge(26);
    8. person1.setBirthday(new Date());
    9. /**两种方式都行
    10. * 因为JSONObject继承了JSON*/
    11. String object = JSONObject.toJSONString(person1);
    12. /*String object = JSON.toJSONString(person1);*/
    13. System.out.println(object);
    14. }

    4.3 将JavaBean List转Json字符串

    1. /**
    2. * 将Java Bean List转Json字符串
    3. */
    4. public static void test3() {
    5. Person person1 = new Person();
    6. person1.setName("张三");
    7. person1.setAge(28);
    8. person1.setBirthday(new Date());
    9. Person person2 = new Person();
    10. person2.setName("李四");
    11. person2.setAge(25);
    12. person2.setBirthday(new Date());
    13. List persons = new ArrayList();
    14. persons.add(person1);
    15. persons.add(person2);
    16. String object = JSON.toJSONString(persons);
    17. System.out.println(object);
    18. }

  • 相关阅读:
    苏州科技大学、和数联合获得国家知识产权局颁发的3项发明专利证书
    C++模板初阶
    【目标检测】YOLOv5在Android上的部署
    第1章、温故而知新
    Python SQLite3 教程
    CY3.5/CY5/CY5.5/CY7/CY7.5荧光标记壳多糖/壳质/角素Chitin
    Linux性能优化--性能追踪:受CPU限制的应用程序(GIMP)
    Vue3最佳实践 第七章 TypeScript 中
    【JAVASE系列】02_运算符与控制语句
    EN 12209建筑五金件锁、插销及其锁板—CE认证
  • 原文地址:https://blog.csdn.net/anglemanyi/article/details/127456151