最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JSONObject用法詳解

 更新時(shí)間:2021年12月30日 09:55:20   作者:程序員s  
本文詳細(xì)講解了JSONObject的用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

JSONObject只是一種數(shù)據(jù)結(jié)構(gòu),可以理解為JSON格式的數(shù)據(jù)結(jié)構(gòu)(key-value結(jié)構(gòu)),可以使用put方法給json對(duì)象添加元素。JSONObject可以很方便的轉(zhuǎn)換成字符串,也可以很方便的把其他對(duì)象轉(zhuǎn)換成JSONObject對(duì)象。

pom:

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

1.通過原生生成json數(shù)據(jù)格式。

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數(shù)據(jù)結(jié)構(gòu)生成

     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.通過實(shí)體生成?

        Student student = new Student();
        student.setId(1);
        student.setAge("20");
        student.setName("張三");
        //生成json格式
        System.out.println(JSON.toJSON(student));
        //對(duì)象轉(zhuǎn)成string
        String stuString = JSONObject.toJSONString(student);

4.JSON字符串轉(zhuǎn)換成JSON對(duì)象

String studentString = "{\"id\":1,\"age\":2,\"name\":\"zhang\"}";
 
//JSON字符串轉(zhuǎn)換成JSON對(duì)象
JSONObject jsonObject1 = JSONObject.parseObject(stuString);
 
System.out.println(jsonObject1);

5.list對(duì)象轉(zhuǎn)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轉(zhuǎn)json字符串
        String string = JSON.toJSON(studentLsit).toString();
        System.out.println(string);
 
        //json字符串轉(zhuǎn)listJson格式
        JSONArray jsonArray = JSONObject.parseArray(string);
 
        System.out.println(jsonArray);

阿里的json很好用,還有一個(gè)谷歌Gson也不錯(cuò)。有興趣的可以看一看

到此這篇關(guān)于JSONObject用法詳解的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

义乌市| 盘锦市| 康平县| 成武县| 玛多县| 县级市| 松桃| 兰溪市| 股票| 陆丰市| 辽阳县| 安新县| 安化县| 西贡区| 邹平县| 岐山县| 定陶县| 广河县| 潢川县| 湖北省| 富民县| 霍林郭勒市| 黎平县| 邮箱| 崇州市| 房山区| 龙江县| 开封市| 萍乡市| 安阳市| 嫩江县| 鄂尔多斯市| 周至县| 云安县| 靖安县| 石楼县| 前郭尔| 昌宁县| 盐亭县| 桦甸市| 城步|