hutool工具-----JSON工具-JSONUtil

时间:2024-10-10 07:38:04

文章预览:

  • JSONUtil
  • 介绍
  • 使用
    • 1、JSON字符串创建
    • 2、JSON字符串解析
    • 3、XML字符串转换为JSON
    • 4、JSON转换为XML
    • 5、 JSON转Bean

JSONUtil

介绍

JSONUtil是针对JSONObject和JSONArray的静态快捷方法集合,在之前的章节我们已经介绍了一些工具方法,在本章节我们将做一些补充。

使用

1、JSON字符串创建

可以将任意对象(Bean、Map、集合等)直接转换为JSON字符串。 如果对象是有序的Map等对象,则转换后的JSON字符串也是有序的。

    /**
     * 转换为JSON字符串
     * <p>
     * 被转为JSON的对象
     *
     * @return JSON字符串
     */
    @Test
    void toJsonStrTest() {
        //map
        SortedMap<Object, Object> sortedMap = new TreeMap<Object, Object>() {
            private static final long serialVersionUID = 1L;
            {
                put("attributes", "a");
                put("b", "b");
                put("c", "c");
            }
        };
        //对象
        Student beanString = new Student(1, 1, 1, "张三");

        //集合
        List<Student> list = new ArrayList<>();
        (new Student(1, 1, 1, "张三"));
        (new Student(1, 1, 2, "李四"));
        (new Student(1, 1, 3, "王五"));

        ("(beanString) = " + (beanString));
        ("(sortedMap) = " + (sortedMap));
        ("(list) = " + (list));
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

结果

(beanString) = {"gradeId":0,"studentId":1,"termId":1,"classId":1,"name":"张三"}
(sortedMap) = {"attributes":"a","b":"b","c":"c"}
(list) = [{"gradeId":0,"studentId":1,"termId":1,"classId":1,"name":"张三"},
{"gradeId":0,"studentId":2,"termId":1,"classId":1,"name":"李四"},
{"gradeId":0,"studentId":3,"termId":1,"classId":1,"name":"王五"}]
  • 1
  • 2
  • 3
  • 4
  • 5

如果我们想获得格式化后的JSON,则:

(object);

("(sortedMap) = "
 + (sortedMap));
  • 1
  • 2
  • 3
  • 4

结果

(sortedMap) = {
    "attributes": "a",
    "b": "b",
    "c": "c"
}
  • 1
  • 2
  • 3
  • 4
  • 5

2、JSON字符串解析

    /**
     * JSON字符串转JSONObject对象
     * <p>
     * JSON字符串
     *
     * @return JSONObject
     */
    @Test
    void Test() {
        String html = "{"name":"Something must have been changed since you leave"}";
        JSONObject jsonObject = (html);
        ("("name") = " + ("name"));
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

结果

("name") = Something must have been changed since you leave
  • 1

3、XML字符串转换为JSON

    /**
     * XML字符串转为JSONObject
     *
     *  XML字符串
     * @return JSONObject
     */
    @Test
    void parseFromXmlTest() {
        String s = "<sfzh>123</sfzh><sfz>456</sfz><name>aa</name><gender>1</gender>";
        JSONObject json = (s);
        ("("sfzh") = " + ("sfzh"));
        ("("name") = " + ("name"));

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

结果

("sfzh") = 123
("name") = aa
  • 1
  • 2

4、JSON转换为XML

    /**
     * 转换为XML字符串
     *
     * @param json JSON
     * @return XML字符串
     */
    @Test
    void toXmlStrTest() {
        final JSONObject put = ()
                .set("aaa", "你好")
                .set("键2", "test");

// <aaa>你好</aaa><键2>test</键2>
        final String s = (put);
        ("s = " + s);

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

结果

s = <aaa>你好</aaa><键2>test</键2>
  • 1

5、 JSON转Bean

我们先定义两个较为复杂的Bean(包含泛型

    @Data
    public class ADT {
        private List<String> BookingCode;
    }

    @Data
    public class Price {
        private List<List<ADT>> ADT;
    }

    /**
     * JSON字符串转为实体类对象,转换异常将被抛出
     *
     * @param <T>        Bean类型
     * @param jsonString JSON字符串
     * @param beanClass  实体类对象
     * @return 实体类对象
     * @since 3.1.2
     */
    @Test
    void toBeanTest() {
        String json = "{"ADT":[[{"BookingCode":["N","N"]}]]}";
        Price price = (json, );
        ("price = " + price);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25