欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JSONObject用法詳解

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

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

        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)文章

  • 超詳細(xì)的JavaScript基本語法規(guī)則

    超詳細(xì)的JavaScript基本語法規(guī)則

    這篇文章主要介紹了JavaScript基本語法規(guī)則,保姆級的詳細(xì)教程,萬字長文詳細(xì)的列出了JavaScript的各種語法,建議收藏系列,希望可以有所幫助
    2021-08-08
  • js分頁代碼分享

    js分頁代碼分享

    這篇文章主要介紹了js分頁代碼示例,很簡單的代碼,需要的朋友可以參考下
    2014-04-04
  • Javascript模塊化編程(一)模塊的寫法最佳實(shí)踐

    Javascript模塊化編程(一)模塊的寫法最佳實(shí)踐

    Javascript模塊化編程,已經(jīng)成為一個迫切的需求。理想情況下,開發(fā)者只需要實(shí)現(xiàn)核心的業(yè)務(wù)邏輯,其他都可以加載別人已經(jīng)寫好的模塊但是,Javascript不是一種模塊化編程語言,它不支持類class,更遑論模塊module了
    2013-01-01
  • 最新評論