• 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. }

  • 相关阅读:
    清朝皇帝年表及1840年后清朝历史事件
    PIL中的P模式、P模式转为L模式
    Linux的关机和重启
    Selenium使用注意事项
    【工程伦理】脑机接口技术中的伦理问题分析
    契约锁“7大”签约避坑指南,帮您化解99%的合同签署麻烦
    DBA_IND_STATISTICS 字段含义
    Java注解@Transa1ctional失效特殊情况
    深刻理解JAVA并发中的有序性问题和解决之道
    开源低代码平台,JeecgBoot v3.7.0 里程碑版本发布
  • 原文地址:https://blog.csdn.net/anglemanyi/article/details/127456151