
文章插圖
在程序開發(fā)過程中,在參數(shù)傳遞,函數(shù)返回值等方面,越來越多的使用JSON 。JSON(JavaScript Object Notation)是一種輕量級的數(shù)據(jù)交換格式,同時也易于機器解析和生成、易于理解、閱讀和撰寫,而且Json采用完全獨立于語言的文本格式,這使得Json成為理想的數(shù)據(jù)交換語言 。
JSON建構(gòu)于兩種結(jié)構(gòu):“名稱/值”對的集合(A Collection of name/value pairs),在不同的語言中,它被理解為對象(Object), 記錄(record), 結(jié)構(gòu)(struct), 字典(dictionary), 有趣列表(keyed list), 哈希表(hash table)或者關(guān)聯(lián)數(shù)組(associative array) 。
JSONObject依賴:
最后一行需要保留,有兩個jdk版本的實現(xiàn):json-lib-2.1-jdk13.jar和json-lib-2.1-jdk15.jar
<dependency><groupId>net.sf.json-lib</groupId><artifactId>json-lib</artifactId><version>2.4</version><classifier>jdk15</classifier></dependency>使用net.sf.json需要導(dǎo)入的jar包jar包下載:
https://pan.baidu.com/s/1iZiXw55TPwIxYFQQCaR9Gw
JSONObject創(chuàng)建JSONObject,添加屬性
//創(chuàng)建JSONObjectJSONObject json = new JSONObject();//添加屬性json.put("username", "張三");json.put("password", "123");//打印System.out.println(json); //增加屬性json.element("sex", "男");json.put("age", 18);System.out.println(json);根據(jù)key返回輸出System.out.println(json.get("sex"));判斷輸出對象的類型boolean isArray = json.isArray();boolean isEmpty = json.isEmpty();boolean isNullObject = json.isNullObject();System.out.println("是否數(shù)組:"+isArray+", 是否空:"+isEmpty+", 是否空為空對象:"+isNullObject);把JSONArray添加到JSONObject中/把JSONArray添加到JSONObject中JSONArray jsonArray = new JSONArray();jsonArray.add(0, "張三");jsonArray.add(1, "123");//開始添加json.element("student", jsonArray);System.out.println(json);全部代碼:import net.sf.json.JSONArray;import net.sf.json.JSONObject; public class Json { public static void main(String[] args) {//創(chuàng)建JSONObjectJSONObject json = new JSONObject();//添加屬性json.put("username", "張三");json.put("password", "123");//打印System.out.println(json);//增加屬性json.element("sex", "男");json.put("age", 18);System.out.println(json);//根據(jù)key返回System.out.println(json.get("sex"));//判斷輸出對象的類型boolean isArray = json.isArray();boolean isEmpty = json.isEmpty();boolean isNullObject = json.isNullObject();System.out.println("是否數(shù)組:"+isArray+", 是否空:"+isEmpty+", 是否空為空對象:"+isNullObject);System.out.println("=====");//把JSONArray添加到JSONObject中JSONArray jsonArray = new JSONArray();jsonArray.add(0, "張三");jsonArray.add(1, "123");//開始添加json.element("student", jsonArray);System.out.println(json); }}運行結(jié)果:JSONArray創(chuàng)建JSONArray,添加屬性值
//創(chuàng)建JSONArrayJSONArray jsonArray = new JSONArray();//添加jsonArray.add(0, "張三");jsonArray.add(1, "123");jsonArray.element("男");System.根據(jù)下標(biāo)返回輸出System.out.println(jsonArray.get(0));根據(jù)下標(biāo)設(shè)置新值,修改jsonArray.set(0, "李四");System.out.println(jsonArray);把JSONObject放入到JSONArray中//把JSONObject放入到JSONArray中JSONObject jsonObject = new JSONObject();jsonObject.put("username", "張三");jsonObject.put("password", "123");jsonArray.add(jsonObject);System.全部代碼:import net.sf.json.JSONArray;import net.sf.json.JSONObject; public class Json { public static void main(String[] args) {//創(chuàng)建JSONArrayJSONArray jsonArray = new JSONArray();//添加jsonArray.add(0, "張三");jsonArray.add(1, "123");jsonArray.element("男");System.out.println(jsonArray);//根據(jù)下標(biāo)返回輸出System.out.println(jsonArray.get(0));//根據(jù)下標(biāo)設(shè)置新值,修改jsonArray.set(0, "李四");System.out.println(jsonArray);//把JSONObject放入到JSONArray中JSONObject jsonObject = new JSONObject();jsonObject.put("username", "張三");jsonObject.put("password", "123");jsonArray.add(jsonObject);System.out.println(jsonArray);//循環(huán)輸出for(int i = 0; i < jsonArray.size(); i++) {System.out.println(jsonArray.get(i));} }}
以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請您及時就醫(yī)或請專業(yè)人士給予相關(guān)指導(dǎo)!
「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對您有所幫助:
- java電商項目模塊介紹 java電商項目面試
- 華為手機紅外攝像頭介紹 被動紅外探測器的工作原理
- 考研筆試分?jǐn)?shù)線
- 三叉神經(jīng)疼怎么引起的呢
- 曲師大美術(shù)考研真題
- 考研報名專業(yè)研究方向
- 夢見自己結(jié)婚辦酒席的含義是好是壞?
- 如何解讀做夢夢到懷孕的象征含義?
- 考研幫曬分
- 考研報名點是什么
