• JsonObject


    什么是JSONObject?

    JSONObject只是一种数据结构,可以理解为JSON格式的数据结构(key-value 结构),可以使用put方法给json对象添加元素。

    JSONObject可以很方便的转换成字符串,也可以很方便的把其他对象转换成JSONObject对象。

    JSONObject相关依赖jar包

    1、方法一(直接下载)

    下载地址(含1、2两种架包)http://download.csdn.net/download/justinqin/10158995

    2、方法二(maven pom.xml依赖)

    com.alibaba.fastjson.JSONObject依赖代码

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.28</version>
    </dependency>

    net.sf.json.JSONObject依赖代码

        <!-- JSONObject对象依赖的jar包 -->
            <dependency>
                <groupId>commons-beanutils</groupId>
                <artifactId>commons-beanutils</artifactId>
                <version>1.9.3</version>
            </dependency>
            <dependency>
                <groupId>commons-collections</groupId>
                <artifactId>commons-collections</artifactId>
                <version>3.2.1</version>
            </dependency>
            <dependency>
                <groupId>commons-lang</groupId>
                <artifactId>commons-lang</artifactId>
                <version>2.6</version>
            </dependency>
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.1.1</version>
            </dependency>
            <dependency>
                <groupId>net.sf.ezmorph</groupId>
                <artifactId>ezmorph</artifactId>
                <version>1.0.6</version>
            </dependency>
            <dependency>
                <groupId>net.sf.json-lib</groupId>
                <artifactId>json-lib</artifactId>
                <version>2.2.3</version>
                <classifier>jdk15</classifier><!-- 指定jdk版本 -->
            </dependency>
            <!-- Json依赖架包下载 -->

    JSONObject基本使用

    1.通过原生生成json数据格式。

        JSONObject zhangsan = new JSONObject();
            try {
                //添加
                zhangsan.put("name", "张三");
                zhangsan.put("age", 18.4);
                System.out.println(zhangsan.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }

    2.通过hashMap数据结构生成

           HashMap<String, Object> map= new HashMap<>();
            map.put("name", "张三");
            map.put("age", 18.4);
            System.out.println(new JSONObject(map).toString());

    3.通过实体生成 

            Student student = new Student();
            student.setId(1);
            student.setAge("20");
            student.setName("张三");
            //生成json格式
            System.out.println(JSON.toJSON(student));
            //对象转成string
            String stuString = JSONObject.toJSONString(student);    

    4.JSON字符串转换成JSON对象

    String studentString = "{"id":1,"age":2,"name":"zhang"}";
     
    //JSON字符串转换成JSON对象
    JSONObject jsonObject1 = JSONObject.parseObject(stuString);
     
    System.out.println(jsonObject1);

    5.list对象转listJson

        ArrayList<Student> studentLsit = new ArrayList<>();
            Student student1 = new Student();
            student1.setId(1);
            student1.setAge("21");
            studentLsit.add(student1);
     
            Student student2 = new Student();
            student2.setId(2);
            student2.setAge("22");
            studentLsit.add(student2);
     
            //list转json字符串
            String string = JSON.toJSON(studentLsit).toString();
            System.out.println(string);
     
            //json字符串转listJson格式
            JSONArray jsonArray = JSONObject.parseArray(string);
            System.out.println(jsonArray);

    阿里的json很好用,还有一个谷歌Gson也不错。有兴趣的可以看一看!

    我话讲完!谁赞成?谁反对?
  • 相关阅读:
    二分图那套理论
    洛谷P4351 [CERC2015]Frightful Formula【组合计数】
    「AGC023E」Inversions【组合计数】
    类欧几里得算法及其拓展
    OLAP 一些扯淡
    auto vectorized case shift
    备忘录
    lambda function pointer
    C++ atomic
    gdb 使用了 O0 但是还是有 <optimized out>
  • 原文地址:https://www.cnblogs.com/wffzk/p/14874569.html
Copyright © 2020-2023  润新知