浠€涔堟槸JSON鍛�

1锛孞SON鏄粈涔堬紵

鏈変竴绉嶅彨鍋欽SON (JavaScript Object Notation) 鐨勮交閲忕骇鏁版嵁浜ゆ崲鏍煎紡鑳藉鏇夸唬XML鐨勫伐浣溿€傚畠灏辨槸JSON銆�

鏁版嵁鏍煎紡姣旇緝绠€鍗曪紝 鏄撲簬璇诲啓锛� 鏍煎紡閮芥槸鍘嬬缉鐨勶紝 鍗犵敤甯﹀灏忥紝鏄撲簬瑙f瀽杩欑璇█銆�

瀹㈡埛绔疛avaScript鍙互绠€鍗曠殑閫氳繃eval()杩涜JSON鏁版嵁鐨勮鍙栵紝鍖呮嫭ActionScript锛� C锛� C#锛� ColdFusion锛孞ava锛孞avaScript锛孭erl锛孭HP锛孭ython锛孯uby绛夎瑷€鏈嶅姟鍣ㄧ璇█锛� 渚夸簬鏈嶅姟鍣ㄧ鐨勮В鏋愩€�

鍚勮瑷€瀵笿SON鏀寔鐨勭壒鍒ソ锛岃嚜浠嶢jax鐨勬祦琛岋紝JSON鏍煎紡浼犺緭灏辨洿娴佽浜嗐€侸SON甯歌鐢ㄤ綔搴忓垪鍖栵紝鎺ㄨ崘闃呰锛�鍏充簬Java搴忓垪鍖栦綘搴旇鐭ラ亾鐨勪竴鍒�銆�


2锛屽鏋滃埌杩欓噷浣犺繕涓嶆槑鐧斤紵

JSON鏄粈涔堬紝閭d箞鎴戝氨鍙戝ぇ鎷涗簡锛�

鍏跺疄鎴戝湪涓哄叕鍙搁潰璇曠殑鏃跺€欙紝鎴戠粡甯告湁鎻愬埌锛屽亣濡傛垜涓嶇煡閬揓SON鏄釜浠€涔堢帺鎰忥紵浣犳€庝箞鍛婅瘔鎴戯紝璁╂垜鐭ラ亾鏄釜浠€涔堜笢瑗裤€傦紙涓嬮潰閮芥槸鎴戠殑涓汉鐞嗚В锛屽洜鐞嗚В鍥犱汉鑰屽紓锛屽彈鐢ㄥ嵆鍙紒锛�

闂簡杩欎箞澶氫汉锛屽叾瀹炵粨鏋滀笉鏄垜鎯宠鐨勶紝澶у鏁扮殑绛旀鏈夎繖浜涖€�

瀹冩槸鍓嶇鍜屽悗鍙颁氦浜掔敤鐨勩€�
瀹冩槸閿€煎鐨勩€�

浠ヤ笂2鐐规瘮杈冨銆傝繕鏈変竴浜涗贡涓冨叓绯熺殑瀹屽叏鍋忕浜咼SON銆傞偅涔堝畠搴旇鏄€庢牱鐨勶紵搴旇杩欎箞鍥炵瓟锛屼釜浜鸿涓猴紒

棣栧厛锛屽畠鍙槸涓€涓瓧绗︿覆锛屽畠鍙槸涓€涓湁瑙勫垯鐨勫瓧绗︿覆銆傦紙閲嶇偣锛夌劧鍚庡畠鐨勮〃杈撅紙琛ㄧ幇锛夊舰寮忔槸閿€煎鐨勩€傚叾瀹為潪甯哥被浼糐ava璇█閲岀殑Map锛孫bjective-C閲岀殑瀛楀吀锛屽叾浠栬瑷€閮芥湁瀵瑰簲鐨勶紝鎴戣瀹炶瘽鍏朵粬璇█鎴戜笉鐭ラ亾锛�

鎴戜负浠€涔堢敤MapString娉涘瀷锛屽洜涓虹悊璁轰笂瀹冪殑閿€煎搴旇閮芥槸瀛楃涓诧紝Why锛熻繖涓棶棰橀棶鐨勫ソ锛屾垜鍙互杩欎箞鍥炵瓟浣狅紝鍥犱负瀹冩湰韬氨鏄瓧绗︿覆銆�

涓婇潰璇寸殑鐞嗚涓婅〃杈炬柟寮忥紝閭d箞瀹為檯鐩墠鎴戜滑鐢ㄥ埌鐨勯敭鍊煎鏀寔浠€涔堝憿锛熷彲浠ユ敮鎸丼tring銆丯umber銆丄rray浠ュ強Boolean銆乶ull浠€涔堢殑銆�

瀹冪殑浣滅敤鏄敤鏉ヤ氦浜掔殑锛屼笉涓€瀹歐eb椤圭洰鐨勫墠鍚庣浜や簰涔熷彲浠ユ帴鍙o紝閰嶇疆鏂囦欢锛屾枃浠跺瓨鍌ㄧ瓑绛夐兘OK銆傜洰鍓嶇Щ鍔ㄧ姣旇緝鐏紝涓€鑸殑椤圭洰閮戒細鐢↗SON鏉ヤ紶杈撱€�


3锛屽畠鑳藉甫鏉ヤ粈涔堬紵

  • 绠€娲併€佺畝鍗曘€佷綋绉皬绛夈€�

  • 涓婃墜瀹规槗锛岄珮鏁堛€�

  • 璺ㄨ瑷€锛岀洰鍓嶇Щ鍔ㄧAndroid銆両OS涓€鑸」鐩瀯閫犻兘鏄叾浠栬瑷€鎻愪緵鎺ュ彛锛圝SON鏂瑰紡锛夛紝绉诲姩绔鍙栨帴鍙g殑鏂瑰紡寮€鍙戙€�
    鈥�.


4锛屽畠鍜孹ML瀵规瘮锛�

鍙鎬�

鐪嬩簡缃戜笂鐨勫悇绉嶅笘瀛愶紝鏈夎鍙鎬х浉褰擄紝涓嶈繃鍩烘湰鏄XML鍙鎬уソ锛屾垜璁や负杩樺噾鍚堛€傚彲浠ユ墦骞虫墜銆�

鍙墿灞曟€�

鏈変汉璇达紝鈥淴ML澶╃敓鏈夊緢濂界殑鎵╁睍鎬э紝JSON褰撶劧涔熸湁锛屾病鏈変粈涔堟槸XML鑳芥墿灞曪紝JSON涓嶈兘鐨勩€�

涓嶈繃 JSON鍦↗avascript涓诲満浣滄垬锛屽彲浠ュ瓨鍌↗avascript澶嶅悎瀵硅薄锛屾湁鐫€XML涓嶅彲姣旀嫙鐨勪紭鍔库€濄€傛垜璧炲悓涓€鍗婏紝鎴戣寰楄繖浜涘笘瀛愰兘鏄€佸笘瀛愶紝XML鍙互鐏垫椿鎵╁睍鏄洜涓哄悇绉嶈瑷€鏈夋敮鎸佺殑鍏朵粬Jar鍖咃紝绫诲簱绛夈€�

浣嗚嚜浠嶫SON鐏捣鏉ュ悗锛孞SON鐨勬敮鎸佸寘锛岀被搴撶瓑锛岀浉淇℃瘡涓瑷€閮藉緢澶氥€傚儚Java灏辨湁Jackson銆乬oson銆乯son-lib銆丗astJson绛夌瓑澶氱殑鍘讳簡銆�

缂栫爜闅惧害

XML鏈夋湁DOM锛孲AX锛孲TAX绛夎В鏋愭妧鏈紝JSON涔熸湁鎴戜笂闈㈣鐨勯偅浜涳紝閮絆K銆�

瑙g爜闅惧害

鍚屼笂銆�

瑙g爜缂栫爜鏁堢巼

鍛靛懙锛佽В鏋怷ML鏈変笓鐢–PU銆佷綘閫犲悧锛�


5锛孌EMO


XML Demo

<country>
    <name>涓浗</name>
    <province>
        <name>榛戦緳姹�</name>
        <citys>
            <city>鍝堝皵婊�</city>
            <city>澶у簡</city>
        </citys>
    </province>
    <province>
        <name>骞夸笢</name>
        <citys>
            <city>骞垮窞</city>
            <city>娣卞湷</city>
            <city>鐝犳捣</city>
        </citys>
    </province>
    <province>
        <name>鍙版咕</name>
        <citys>
            <city>鍙板寳</city>
            <city>楂橀泟</city>
        </citys>
    </province>
    <province>
        <name>鏂扮枂</name>
        <citys>
            <city>涔岄瞾鏈ㄩ綈</city>
        </citys>
    </province>
</country>


JSON Demo


{
    "name""涓浗"锛�
    "province": [
        {
            "name""榛戦緳姹�"锛�
            "citys": {
                "city": [
                    "鍝堝皵婊�"锛�
                    "澶у簡"
                ]
            }
        }锛�
        {
            "name""骞夸笢"锛�
            "citys": {
                "city": [
                    "骞垮窞"锛�
                    "娣卞湷"锛�
                    "鐝犳捣"
                ]
            }
        }锛�
        {
            "name""鍙版咕"锛�
            "citys": {
                "city": [
                    "鍙板寳"锛�
                    "楂橀泟"
                ]
            }
        }锛�
        {
            "name""鏂扮枂"锛�
            "citys": {
                "city": [
                    "涔岄瞾鏈ㄩ綈"
                ]
            }
        }
    ]
}

6锛孞ava甯哥敤鐨刯son搴�

鎴戜滑鍦ㄦ棩甯稿紑鍙戜腑灏戜笉浜嗗拰JSON鏁版嵁鎵撲氦閬擄紝閭d箞鎴戜滑鏉ョ湅鐪婮AVA涓父鐢ㄧ殑JSON瑙f瀽鏂瑰紡銆侸SON甯歌鐢ㄤ綔搴忓垪鍖栵紝鎺ㄨ崘闃呰锛�鍏充簬Java搴忓垪鍖栦綘搴旇鐭ラ亾鐨勪竴鍒�銆�

  • JSON瀹樻柟

  • GSON

  • FastJSON

  • Jackson

鏈珯澹版槑

鈶犳湰绔欓儴鍒嗗伐鍏烽兘鏉ユ簮浜庣綉缁滃拰缃戝弸鎻愪緵锛屽鏈夌増鏉冮棶棰橈紝璇疯仈绯绘湰绔欙紒

鈶℃湰绔欏伐鍏蜂粎涓轰簡澶у鏂逛究浣跨敤锛岃鍕跨敤浜庤繚娉曘€佸晢鍔°€佷氦鏄� 绛夋柟闈紒鍚﹀垯鏈珯姒備笉璐熻矗锛�