Java常用類---JSONObject


(1).什么是JSONObject?
JSONObject只是一種數據結構,可以理解為JSON格式的數據結構(key-value 結構),可以使用put方法給json對象添加元素。

JSONObject可以很方便的轉換成字符串,也可以很方便的把其他對象轉換成JSONObject對象。

 

(2).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依賴架包下載 -->

 

(3).JSONObject基本使用

1.通過原生生成json數據格式。

JSONObject zhangsan = new JSONObject();
        try {
            //添加
            zhangsan.put("name", "張三");
            zhangsan.put("age", 18.4);
            zhangsan.put("birthday", "1900-20-03");
            zhangsan.put("majar", new String[] {"哈哈","嘿嘿"});
            zhangsan.put("null", null);
            zhangsan.put("house", false);
            System.out.println(zhangsan.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

 

2.通過hashMap數據結構生成

     HashMap<String, Object> zhangsan = new HashMap<>();
        
        zhangsan.put("name", "張三");
        zhangsan.put("age", 18.4);
        zhangsan.put("birthday", "1900-20-03");
        zhangsan.put("majar", new String[] {"哈哈","嘿嘿"});
        zhangsan.put("null", null);
        zhangsan.put("house", false);
        System.out.println(new JSONObject(zhangsan).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("20");
        student1.setName("asdasdasd");
 
        studentLsit.add(student1);
 
        Student student2 = new Student();
        student2.setId(2);
        student2.setAge("20");
        student2.setName("aaaa:;aaa");
 
        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也不錯。有興趣的可以看一看

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM