資源描述:
《java中arraylistmapobject與json互相轉(zhuǎn)換》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫(kù)。
1、JSON(JavaScriptObjectNotation):是一種輕量級(jí)的數(shù)據(jù)交換格式一、JSON建構(gòu)有兩種結(jié)構(gòu):對(duì)象和數(shù)組1、對(duì)象:對(duì)象在js中表示為“{}”擴(kuò)起來(lái)的內(nèi)容,數(shù)據(jù)結(jié)構(gòu)為{key:value,key:value,...}的鍵值對(duì)的結(jié)構(gòu),在面向?qū)ο蟮恼Z(yǔ)言中,key為對(duì)象的屬性,value為對(duì)應(yīng)的屬性值,所以很容易理解,取值方法為對(duì)象.key獲取屬性值,這個(gè)屬性值的類型可以是數(shù)字、字符串、數(shù)組、對(duì)象幾種。2、數(shù)組:數(shù)組在js中是中括號(hào)“[]”擴(kuò)起來(lái)的內(nèi)容,數(shù)據(jù)結(jié)構(gòu)為["java","javascript","vb",...],取值方式和所有語(yǔ)言中一
2、樣,使用索引獲取,字段值的類型可以是數(shù)字、字符串、數(shù)組、對(duì)象幾種。經(jīng)過(guò)對(duì)象、數(shù)組2種結(jié)構(gòu)就可以組合成復(fù)雜的數(shù)據(jù)結(jié)構(gòu)了。二、具體形式1、對(duì)象(1)一個(gè)對(duì)象以“{”(左括號(hào))開始,“}”(右括號(hào))結(jié)束。(2)每個(gè)“名稱”后跟一個(gè)“:”(冒號(hào))(3)“‘名稱/值’對(duì)”之間使用“,”(逗號(hào))分隔例子:表示人的一個(gè)對(duì)象:{"姓名":"大憨","年齡":24}2、數(shù)組是值(value)的有序集合。(1)一個(gè)數(shù)組以“[”(左中括號(hào))開始,“]”(右中括號(hào))結(jié)束。(2)值之間使用“,”(逗號(hào))分隔。例子:一組學(xué)生{"學(xué)生":[{"姓名":"小明","年齡":23},{"姓名"
3、:"大憨","年齡":24}]}說(shuō)明:此Json對(duì)象包括了一個(gè)學(xué)生數(shù)組,而學(xué)生數(shù)組中的值又是兩個(gè)Json對(duì)象。說(shuō)了這些基本了解json的數(shù)據(jù)結(jié)構(gòu)了...補(bǔ)充:在線Json校驗(yàn)格式化工具:http://www.bejson.com/go.php?u=http://www.bejson.com/index.php三、老樣子上次demo這時(shí)我的工程結(jié)構(gòu)圖:上面引用到的外部庫(kù)大家網(wǎng)上搜索下載~configdata.json:[javascript]?viewplaincopy1.[??2.????true,??3.????false,??4.????true??5.]
4、??Address類:[java]?viewplaincopy1./**????2.?*?@Title:?創(chuàng)建Address實(shí)體類的POJO?1.?*?@Description:?TODO(用一句話描述該文件做什么)?2.?*?@author?Potter????3.?*?@date?2013-2-18?上午10:16:03?4.?*?@version?V1.0????5.?*/??6.??7.public?class?Address?{??8.????private?String?street;//街道??9.????private?String?city;
5、//城市??10.????private?int?zip;//郵編??11.????private?String?tel;//第一個(gè)電話號(hào)碼??12.????private?String?telTwo;//第二個(gè)電話號(hào)碼??13.??14.????public?Address()?{??15.????}??16.??17.????public?Address(String?street,?String?city,?int?zip,?String?tel,?String?telTwo){??18.????????this.street?=?street;??19
6、.????????this.city?=?city;??20.????????this.zip?=?zip;??21.????????this.tel?=?tel;??22.????????this.telTwo?=?telTwo;??23.????}??24.??????25.????public?String?getStreet()?{??26.????????return?street;??27.????}??28.??29.????public?void?setStreet(String?street)?{??30.????????this.stree
7、t?=?street;??31.????}??32.??33.????public?String?getCity()?{??34.????????return?city;??35.????}??36.??37.????public?void?setCity(String?city)?{??38.????????this.city?=?city;??39.????}??40.??41.????public?int?getZip()?{??42.????????return?zip;??43.????}??1.??2.????public?void?setZip(
8、int?zip)?{??3.?????