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

JSONObject用法詳解

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

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

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

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

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

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

5.list對象轉(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很好用,還有一個谷歌Gson也不錯。有興趣的可以看一看

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

相關(guān)文章

最新評論

定陶县| 察隅县| 清新县| 平安县| 鄱阳县| 嘉鱼县| 潜江市| 毕节市| 石屏县| 大足县| 开远市| 鄂托克前旗| 临江市| 曲阜市| 克拉玛依市| 德州市| 巩义市| 潜山县| 道真| 读书| 富顺县| 杭锦后旗| 盘山县| SHOW| 阜城县| 长葛市| 保德县| 读书| 广饶县| 浦江县| 临江市| 四平市| 正镶白旗| 姚安县| 台州市| 镇江市| 恭城| 静海县| 太仓市| 宁阳县| 莫力|