[{ "MVP":"Stephen Curry", "position":"Point Guard", "number":"30", "team":"Golden State Warriors" },{ "FMVP":"Lebron James", "position":"Small Forward", "number":"23", "team":"Cleveland Cavaliers" }]
更復(fù)雜的數(shù)據(jù)也可以使用JSON來進行表示,比如下面這段信息:最喜歡的編輯器是sublime,最喜歡的運動是籃球,最喜歡的球隊是馬刺隊,馬刺隊控衛(wèi)有托尼帕克、帕蒂米爾斯,分衛(wèi)有馬努吉諾比利、丹尼格林,小前鋒有科懷倫納德、凱爾安德森,大前鋒有拉馬庫斯阿爾德里奇、大衛(wèi)李,中鋒有德維恩戴德蒙、保羅加索爾。
{ "favoriteEditor": "sublime", "favoriteSport": "basketball", "favoriteTeam":{ "Spurs":[ { "PG":"Tony Parker", "SG":"Danny Green", "SF":"Kawhi Leonard", "PF":"LaMarcus Aldridge", "C":"Pau Gasol" }, { "PG":"Patty Mills", "SG":"Manu Ginobili", "SF":"Kyle Anderson", "PF":"David Lee", "C":"Dewayne Dedmon" } ] } }
var str = '[{"name":"james","height":"203","number":"23"}, {"name":"kobe","height":"198","number":"24"}]'; //定義JSON字符串 var oJson = eval(str); //通過eval()函數(shù)解析拿到JavaScript原生值 //接下來便可通過JS進行操作 alert(oJson[0].name); //james alert(oJson[1].height); //198
var str = '[{"name":"james","height":"203","number":"23"},{"name":"kobe","height":"198","number":"24"}]'; //定義JSON字符串 var oJson = JSON.parse(str); //通過JSON對象的parse方法得到該JSON字符串的JavaScript原生值 document.write("姓名:" + oJson[1].name + "<br/>" + "身高:" + oJson[1].height + "cm" + "<br/>" + "球衣號碼:" + oJson[1].number);
var str = '[{"name":"james","height":"203","number":"23"},{"name":"kobe","height":"198","number":"24"}]'; var oJson = JSON.parse(str, function(key, value){ if('name' == key){ return '球員' + value; }else if('number' == key){ return value + '號球衣'; }else{ return value; } }); alert(oJson[0].name + '更牛逼還是' + oJson[1].name + '更牛逼' + '\n' + '你喜歡' + oJson[0].number + '還是' + oJson[1].number);
var oJson = [{ name : "james", height : 203, number : 23 },{ name : "kobe", height : 198, number : 24 }]; var jsonStr = JSON.stringify(oJson,['name','number'],4); alert(jsonStr);
var oJson = [{ name : "james", height : 203, position : '小前鋒' },{ name : "kobe", height : 198, position : '得分后衛(wèi)' }]; var jsonStr = JSON.stringify(oJson,function(key,value){ switch(key){ case 'name': return '球員 ' + value; case 'height': return '身高 ' + value + 'cm'; case 'position': return '位置 ' + value; default: return value; } },4); alert(jsonStr);
支持JSON對象的瀏覽器有IE8+、Chrome、FireFox3.5+、Opera10.5+、Safari4+,不支持的瀏覽器可以通過json.js這一開源庫來進行模擬。
聯(lián)系客服