柚子快報(bào)激活碼778899分享:JSON 互相轉(zhuǎn)化
柚子快報(bào)激活碼778899分享:JSON 互相轉(zhuǎn)化
一. JSON
1.JSON 介紹
JSON 是一種純字符串形式的數(shù)據(jù),它本身不提供任何方法(函數(shù)),非常適合在網(wǎng)絡(luò)中進(jìn)行傳輸。JavaScript、PHP、Java、Python、C++ 等編程語言中都內(nèi)置了處理 JSON 數(shù)據(jù)的方法。
JSON 是基于 JavaScript的一個(gè)子集,是一種開放的、輕量級(jí)的數(shù)據(jù)交換格式,采用獨(dú)立于編程語言的文本格式來存儲(chǔ)和表示數(shù)據(jù),易于程序員閱讀與編寫,同時(shí)也易于計(jì)算機(jī)解析和生成,通常用于在 Web 客戶端(瀏覽器)與 Web 服務(wù)器端之間傳遞數(shù)據(jù)。
在 JSON 中,使用以下兩種方式來表示數(shù)據(jù):
Object(對(duì)象):鍵/值對(duì)(名稱/值)的集合,使用花括號(hào) { } 定義。在每個(gè)鍵/值對(duì)中,以鍵開頭,后跟一個(gè)冒號(hào):,最后是值。多個(gè)鍵/值對(duì)之間使用逗號(hào), 分隔,例如{“name”:“C語言中文網(wǎng)”,“url”:“http://c.biancheng.net”}; Array(數(shù)組):值的有序集合,使用方括號(hào) [ ] 定義,數(shù)組中每個(gè)值之間使用逗號(hào), 進(jìn)行分隔。
2. 導(dǎo)入依賴
3. JSON轉(zhuǎn)對(duì)象
Student o = JSONObject.parseObject(jsonString, Student.class);
4. 對(duì)象或者List集合轉(zhuǎn)化為JSON
List
Student deme1 = new Student();
deme1.setDiscipline("022000000000");
deme1.setLongitude(120.680175);
deme1.setLatitude(31.315544);
studentList.add(deme1);
String s2 = JSON.toJSONString(deme1);//對(duì)象轉(zhuǎn)化為json
Student deme2 = new Student();
deme2.setDiscipline("022000000000");
deme2.setLongitude(120.613318);
deme2.setLatitude(31.381417);
studentList.add(deme2);
String s = JSON.toJSONString(studentList);//List轉(zhuǎn)換為json
二.?JSONObject
1.?JSONObject 介紹
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ì)象。
2.通過原生生成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();
}
3.通過hashMap數(shù)據(jù)結(jié)構(gòu)生成
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());
4.通過實(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);
5.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);
6.?list對(duì)象轉(zhuǎn)listJson
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);
柚子快報(bào)激活碼778899分享:JSON 互相轉(zhuǎn)化
精彩鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。