一、简介:Json是JavaScript中读取结构化数据更好的方式。因为Json数据可以直接传给eval(),而且不必创建DOM对象。Json是一种数据格式,不是一种编程语言,虽然具有相同的语法形式,带Json并不从属于JavaScript.而且,并不是只有JavaScript才使用Json,毕竟Json只是一种数据格式。很多编程语言都有针对Json的解析器和序列化器。
二、语法
Json的语法可以表示一下三种类型的值
1、简单值:使用与JavaScript相同的语法,可以在Json中表示字符串、数值、布尔值和null。注意:Json不支持JavaSciprt中的特殊值:undefined。
2、对象:对象作为一种复杂的数据类型,表示的是一组无序的键值对儿。而每个键值对中的值可以是简单值,也可以是复杂数据类型的值。
3、数组:数组也是一种复杂的数据类型,表示一组有序的值的列表,可以通过数值索引来访问其中的值。数组的值也可以是简单值、对象、数组。
2.1 简单值
简单值在Json中的表示可以是字符串、数值、布尔值和null!
注意:上面这些数据都和JavaScript中一样,只有字符串,在Js中字符串可以用单引号表示,但是在Json中字符串使用单引号会报错!
2.2 对象
Js中的对象,代码如下:
var person={
name:"张三",
age:23
}; var person={
"name":"张三",
"age":23
};
在JS中上面两种方法都可以表示对象,但是在Json中没有变量的概念。其次,没有末尾的分号(因为这不是JavaScipt语句,所以也不需要分号),
所以在Json的对象中,对象的属性必须是字符串(也就是说要加双引号),属性的值可以是简单值,数组,对象,所以在Json中的对象表现形式如下:
{
"name":"张三",
"age":23
}
注意:对象的属性名必须加双引号!
三、Json的解析与序列化
JSON之所以流行,拥有与JavaScript类似的语法并不是全部的原因,更重要的原因是,可以把Json数据结构解析为有用的JavaScipt对象。与XML数据解析成DOM文档而且从中提取数据麻烦相比,JSON可以解析为JavaScript对象的优势十分明显!代码如下:
[
{"title":"book_one",
"year":"2016"
},
{"title":"book_two",
"year":"2015"
},
{"title":"book_three",
"year":"2015"
},
]
下面需要取得第三本书的名字,通过Json转换为JavaScript对象的方式获取
books[2].title
通过XML解析成DOM元素,在获取:
document.getElementsByTagName("book")[2].getAttribute("title");
比较两种方法,就知道Json为什么会得到JavaSciprt开发人员的欢迎了!
早期的JSON解析器基本上是使用JavaScipt的eval()函数。由于Json是JavaScript语法的子集,因此eval()函数可以解析、解释并返回JavaScript对象和数组。在旧版本的浏览器中,使用Eval()对JSON数据结构求值存在风险,可能会执行一些恶意代码。对于不能支持原生JSON解析的浏览器,使用shim是最佳选择。地址: shim:https://github.com/douglascrockford/JSON-js;
3.1 JSON对象
ECMAScript 对解析JSON的行为进行了了规范,定了全局对象JSON;
支持两个方法:stringify()和parse()。在最简单的情况下,这两个方法风别用于把JavaScript对象序列化为JSON字符串和把JSON字符串解析为原生JavaScript值。
1、stringify()
var book={
title:"book_title",
authors:["张三","李四"],
edition:3,
year:2016
}
var jsonText=JSON.stringify(book);
alert(jsonText);
输出:{"title":"book_title","authors":["张三","李四"],"edition":3,"year":2016}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAaIAAABNCAIAAABrDPfbAAAGGklEQVR4nO2bwZHiMBAAHRG8SGT3vQHsmyICMqCIgS9VJMHHVZfGhXEPFXODNBoLW7DHXFP92NUaSyON25LsHb6/v8ffvwAAojKgOQCIDZoDgOCgOQAIDpoDgOCgOQAIDpoDgOCgOQAIDpoDgOCgOQAIDpoDgOCgOQAIDpoDgOCgOQAIDpoDgOCgOQAIDpoDgOCgOQAIDpoDgOCgOQAIDpoDgOCgOQAIDpoDgOCgOQAIDpoDgOCgOQAIDpoDgOCgOQAIDpoDgOCgOQAIDpoDgOCgOQAIDpoDgOCgOQAIDpoDgOC8SHPn62UYhvP1klc/DMtPvtqsD6fji8/z1IjmYbbnZXx8fQ63z/l6OZyO2/1O921q22qzHuqf2bVv9ztdna73GdVNcjgdV5v1WMmTWvLAk+ijuWEYUpKtNuvtfpcGWDNPCrWzZeVvpLnGiGaPQscrp9akpLOPr8/aMR9fn1pzh9Px0Ya19FLqf+cjVtWt0iX6Bzltim75AD1Jc04zHHd3ya6H2qNHR19T2ehIX2WHSaFEtN3vltyWOmjucDqmpB9///r4+pwUkC70m56drVb+Ss29JqJ5be6rOaepUqN5jN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V468HmxMxs1XZ4OrVX/Z+n5M8tQGqjaY5KOXq0u+3bG1be9pQ5luL5spVcLaBmOWhPjK7AIfKU6ZH6fzeHITvN+dJq1zh482b+q9pFeNIZ7Ak29hLjuZqX5mczb3pALX02+xNrh/kh9+bG+8n3pHILsK+e0z6DYw3Qv8XxAuY7CX/rYt50a1uL9m84wBN9tuTthGeHc6SNvOv+wD/Edn7hv8JaA4AgoPmACA4aA4AgoPmACA4aA4AgoPmACA4aA4AgoPmACA4aA4AgoPmACA4aA4AgoPmACA4aA4AgoPmACA4aA4AgoPmACA4aA4AgoPmACA4aA4AgoPmACA4aA4AgoPmACA4aA4AgoPmACA4aA4AgoPmACA4aA4AgoPmACA4aA4AgvMHaVWryg/UpkcAAAAASUVORK5CYII=" alt="" />
这里需要注意,在默认情况下(也就是不提供第三个参数的情况下)JSON.stringify()输出的JSON字符串不包含任何的空格字符或缩进,但是如果你提供第三个参数,就是JSON字符串添加字符缩进的话,那么所返回的JSON字符串就会产生缩进,代码如下:
var book={title:"book_title",authors:["张三","李四"],edition:3,year:2016}
var jsonText=JSON.stringify(book,null,4);
alert(jsonText);
输出图如下:aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAANkAAACuCAIAAAA0+z9YAAAH30lEQVR4nO2dwXHrLBRGVVGySiPJOlV4XIE7yKSGbDOTJrJhJm28Mv7F/UOuASmSrIhPcDxn4fBsjMR5IOAKDeHfF4ACQ/USABi4CCrgIqiAi6ACLoIKuAgq4CKogIugAi6CCrgIKuAiqICLoAIuggq4CCrgIqig6OIwDKfLuXoxYGfkXHx5e318fqpeDNgfORdPlzONYp/gIqgg5+Lj8xMu9omWi6fLmYvFbtFyMdAudoyci1wvdgsuggpyLjK/2C1yLgbWXXpF0UXoE1wEFXARVMBFUAEXQYVmXXz//BiG4f3zIz3gYeNDvnu4f3l73Tmf3Y5uTw5cdOPu4f50Od893Cfp62prLLeJzxzIxTlHV5HDu2jr19Nm+MTp2kpyW/2LK5iTzw5HV5EWX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i5SLvYLYou0ij2CS6CCnIuMr/YLVousu7SM1Q8qICLoAIuggq4CCrgIqiAi6ACLoIKuAgq4CKogIugAi6CCv8BHTK89w2Cfq0AAAAASUVORK5CYII=" alt="" />结合上面没有提供第三个参数的情况下,发现给stringify()提供第三个参数的情况下,在每个级别上增加了4个空格,增加了JSON字符串的可读性!
还有一种情况,如下:
var book={title:"book_title",authors:["张三","李四"],edition:3,year:2016}
var jsonText=JSON.stringify(book,null,"--");
alert(jsonText);
输出图如下:aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAO8AAADACAIAAABj6KMNAAAJDElEQVR4nO2dQXIiORBF60Sw8kXsNQfoNcEJ2E53u92cwVtHeDUxF/BGEY65xRxjFjnkpFWqsqooQ2XqEW9hZCgo+Igv5ZfoTqfTt2/f0j/vAN7pfv9GzRCE7vuPn6gZYtA9/vqNmiEGHb4ZwtD9ekLNEITuj+8/UDPEgL4Z4oBvhjgwpwFxYL4Z4kAtEOKAb4Y4dD8fn1AzxKD78fMRNUMMcBoQB9QMccBpQBwYBUIccBoQB6onEAcq2xAH96mjruv2x8PNnwasAd+++en5dL97uPnTgJXgO62/Px7omEHxvZIKNYPFd998v3tAzaA49s374wHTDBbfcxr0zWDxPd+MbwaL71ogagaLY9+cmG+Gj7jP0FELBIV8M8TBt9MAsKBmiANOA+LgfhQIoLToNF7eXruue3l7zV+Lrlv2gTZ326fn05WPc7WzWyG+qyfjbO62++Nhc7fN2ue930NHG7mNIzXXnN368V3ZHkdSHOPaso3j73d2tNmPOIOa41zh7NaP79TROPe7h6fn04Jqtkeb/YgzuI6aPz279dOKb+7Ol83dVr6Ls8b08f3WfxULjUPf5hkiJj2UVVtnLsXnadtVlPK4/WL+smfnF99p/Ulkmsgak3m/rVKLfV69mvWYIms9vsqopl2eg7QMPeiCZ+cX3yuptI+Rtye7mlH5ftve9MIOLNOKijL7Qh9vlz/udw+fDuOufHYrhL658H4vZR/r1fzy9jrULn9INz/ybXD9s1shrfjmNPG7WB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J8Ow0Ai+9RIIDFsdMAyHBfPQFQfFe2ASy+U0cAFnwzxMF3Wh/A4nglFUAGfTPEAd8McWBOA+LAfDPEgVogxAHfDHEgQwdxIN8MccBpQBxQM8QBpwFxYBQIccBpQBz+q578+fdfAN75F0y5K9eK6J6gAAAAAElFTkSuQmCC" alt="" />发现给stringify()提供第三个参数的情况下且为字符串的情况,它会解析字符串的长度,并在每个级别上增加了(字符串长度)个字符串!
JSON.stringify()除了接收第一个参数(要序列化的JavaScript对象外),还可以接收另外两个参数,这两个参数用于指定以不同的方式序列化JavaScript对象。第一个参数是过滤器,可以是一个数组,也可以是一个函数,第二个参数是一个数值或者字符串,用于表示是否在JSON字符串中保留缩进(这个参数在上面已经介绍了),下面是关于stringify()方法的过滤器参数,代码如下:
var book={title:"book_title",authors:["张三","李四"],edition:3,year:2016}
var jsonText=JSON.stringify(book,function(key,value){
switch (key){
case"authors":return value.join("$$")
case"edition":return 3+" version"
case"year":return "2016-10-19"
default:return value;
}
});
alert(jsonText);
输出图:aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgQAAABBCAIAAAAhais3AAAGmklEQVR4nO2bwXHjMAxFVZFzSiPJOVV4XIE78KSGXDOTJnLBzLaxZewBYwxMghAlUfZu9nnewaFDEgApfBGyJ/n9CwAA/nOmh1sAAAAPBzEAAADEAAAAEAMAABDEAAAABDEAAABBDAAAQBADAAAQxAAAAAQxAAAAQQwAAEAQAwAAEMQAAAAEMQAAAEEMAABAEAMAABDEAAAABDEAAABBDAAAQBADAAAQxAAAAAQxAAAAQQwAAEAQAwAAEMQAAAAEMQAAAEEMAABAEAMAABDEAAAABDEAAAC5gxh8fn9N0/T5/VVOPA2Y+vD8dPl4v/M4u3q0jtCeXXl5e52ur8/vr8vH+/F88vFUew7PT1P7VSxBuC4tA47nk5/Rd+mccTuXj3e1MNwSrX3yb/EzvIAetl4e0zTpNXl4fjqeT/XVuy51tkYr2v8hMej0aPUqDLxcF5n68vbqxeDy8b7CGK8r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ZqLkyNDK8LlqW91A8cs9LMXWZqIhwa5YV1Z7ZT6fGDeuDTwaSFqoWFVX8ubseKi8TWRf//arp9jxry9BqV8Na+WW4Ry0ut0+EvBio+97HXAxmnbXY2vtwC86ehf3JQKI6wyw+49tcdWNYIPKv4plBa6J+wnWcbmvW68pEfkPmaTQpH/mdH7qmmcsPG3qU74chXiT0GBleF5ImzTxphI9YrBTTOgAV3ReVifIp8o5Goj2Pf2aQWKBrdrh9jGaN4lKnlT5biXhWDIrDo1y3VH97kVjDdDbco9Y+sNRWzBI22tTh1TjrrLgqylIlttHkKgbT9Xs+i9KuD1dRnSgai16hwYeFPzpL1iJcR1/HME/DLVGMHJrRWrie+OdP/m1MH6XQo3w/DPSiZWRR5moZaVZNG8SgLkN9Vr9iae2lIkVM6Y1j3TGcoqejNSbp/vFiUBTC4P+JW/HtkeKJix5aO+/llVYFo2VAIgatLuFHP2At/n7yIG8vdsPjf2cgtyfHn0SRtrYH2uO/lwkrojd7o8Ra/FUkQV5RV4Q6vENiyDIAwGMofjsCjwUxAAAAxAAAABADAAAQxAAAAAQxAAAAQQwAAEAQAwAAEMQAAAAEMQAAAEEMAABAEAMAABDEAAAABDEAAABBDAAAQBADAAAQxAAAAAQxAAAAQQwAAEAQAwAAEMQAAAAEMQAAAPn96w/RXl2bzkyGvwAAAABJRU5ErkJggg==" alt="" />
当stringify使用第二个参数时,有一个特殊情况,代码如下:
var book={title:"book_title",authors:["张三","李四"],edition:3,year:2016}
var jsonText=JSON.stringify(book,function(key,value){
switch (key){
case"authors":return value.join("$$")
case"edition":return undefined
case"year":return "2016-10-19"
default:return value;
}
});
alert(jsonText);
输出图:aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYsAAABDCAIAAADiTr6xAAAGK0lEQVR4nO2cS24qOxCGe0VklI3AmAV4jFgB45Pw6DVkipRNZNLS3cZdxh1YlAq/2m0Mp6L7oW+QGNxll+2/y0XjYfr3HwAAUzjn/nwc/3wch7/eFACAAOfc+TKe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yZ0xbKJQkVvRXZ5Q5TbSl6/jZY8VNlw5XqOZ/AiuU0GvXHYXC73f/ipX2PrqT2aoqCmUqH0yvHbrkJn42Bz6SZRK8V0U6jkvqx+KH3cNFsoeG/EVab7CEiaurRtWuvLIxvHoY/kH9bbjVa9eoUKQk7/SWmYHuKaGGq2ovaJH6ncLCqrXg2N3+U9HkM9uO2vV6jcopVUhb/p1dx2ntcjf2XvUrHSUaGks8mEWoNOTTdFkDxUc1pUlCVYD3GhdkWyI7lpuXRcJCr0Czg5sskhyzm5Bi1PsdFCHiq4PXTfrM2+m7vB94qheuahpur1PKmvAyYVMMeTILfLkyr6e83hfoOjkwjJct+w3KTv3qMcQX5RK5Tvvu5jWaFmOyu+DVIn9c7XvRaFmt02Jh0rzRZbcWFyf6dfQR4qZ6gev+a1PsYjmxyynDNnXapDv3jiFeLioPqiXV7ZRLmiUBDEXnmollOAC7b9aK1UXlkXTmo9554BqRzRYLsh6Lk7Wx6s9uQa696j3AyQ9RZYSRaK6Vihajo7qU3Q0tuDXG26KdRw+5ZtkRYkn6lJFga1kg1eLXxisyypwR4zlxSPR6dBoeJd5DV6ECzXzWD25iZermLSRE1FKSxoUC+FavldXrCDhf+P30Q0ZXOnJ7cP7CujHk9S1ApKV1CoXJXkW+WxeDyBAr2eh2o8Bbgsn7+XYC097mKN/qoeGrw3e9/uMhYNO1aI3dvFh5wPBXBH8PgV/F04YxMA7IJCAYBd2OUBgF04BRgA7MIpwABgF04BBgC7kIcCALu0nwIMAPBsGk8BBgB4AY0n2AEAvADyUABgF2IoALALeSgAsAvf5QGAXchDAYBdGk8BBgB4AfwuDwDs0n4KMADAsyEPBQB24YxNALALCgUAdpFd3n+m1pT18HnvlgAAAABJRU5ErkJggg==" alt="" />
当给edition属性赋了undefined值的时候,JSON.stringify()返回JSON字符串对象时,去除了属性值为undefined的属性!
有时候,JSON.stringify()还不能满足对某些对象进行自定义序列化的需求。在这些情况下,可以给对象定义toJSON()方法,返回其自身的JSON格式数据。原生的Date对象有一个toJSON()方法,能够将JavaScript的Date对象自动转换为日期字符串,代码如下:
var book={title:"book_title",authors:["张三","李四"],edition:3,year:2016,toJSON:function(){
return this.title;
}}
var jsonText=JSON.stringify(book);
alert(jsonText);
输出图:aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOkAAABVCAIAAAAjXVS6AAACNklEQVR4nO3XQW7aQBiGYZ8IVlwE1pwCcQJuEOUM2SJlFXVZVWJjCXXfA/QYXUyxhgFTklahn/xYz4L8sZA1eWOPu/7nERJ1D78C+Bjtkkq7pNIuqbRLKu2SSruk0i6ptEsq7ZJKu6TSLqm0Syrtkkq7pNIuqbRLKu2SSruk0i6ptEsq7ZJKu6TSLqm0Syrtkkq7pNIuqbRLKu2SSruk0i6ptEsq7ZJKu6TSbmu2mD+9PH/y9+wPr13X7Q+vzbzr/IFGWZrjbDHf7LazxXz4MaXd5sqnRrvH5Xr1qHavnlMPb7fbXPnUaPe4XK+eXp5D262vfGq02yrpdKejbqurjrNFvDYfEiz7geV61S796Zgt5uWcZtiftzv8arPbPnyV/gfabc0W86GYEvHvlaqiuWde2i2Ty41sfU5/6rsZ9lW79Zf8qydDOu22mjKGBJtH8+15+bBcr/74snVPu/VzwK13oN3W/e3uD69j8/Kh3MLHbrr9e9qd7Kb2Bu22yv2yfN7stmN7gyGmsXlJsEQ59oh/155hGF5unadJu63hWX/nO9nYvEnw6uah/G/U72r1sK/aLSdcvj5OmXZJpd1PXOvzw+3zb9fz4VcAH6NdUmmXVNollXZJpV1SaZdU2iWVdkmlXVJpl1TaJZV2SaVdUmmXVNollXZJpV1SaZdU2iWVdkmlXVJpl1TaJZV2SaVdUmmXVNollXZJpV1SaZdU2iWVdkmlXVJpl1Td2/cvkKj7+uMbJPoF4lB8coOWnzoAAAAASUVORK5CYII=" alt="" />根据这个输出,和上面所有JSON.stringify()方法的输出我们总结出一个规律,那就是:
JSON.stringify()的序列化顺序如下:
(1)如果被序列化的对象存在toJSON()方法而且能通过它取得有效的值,则调用该方法,如果该方法取不到有效的值,则返回对象本身。
(2)如果提供了第二个参数,应用这个函数过滤器。传入函数过滤器的值是第(1)步返回的值。
(3)对第(2)步返回的值进行序列化。
(4)如果提供了第三个参数,则按照对三个参数,对对象执行相应的格式化
2、parse()
var book={
title:"book_title",
authors:["张三","李四"],
edition:3,
year:2016
}
var jsonText=JSON.stringify(book);
alert(JSON.parse(jsonText));
输出:[object Object] ,firebug截图,监视查看对象如图:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARcAAAB0CAIAAAAgpuHDAAAJQUlEQVR4nO3dXW/cxBoHcH+nam/mo+zVXs1F7pEqWikS0rF6OZekKKiXyJVQo3M6RVUpRIGUvsSitIiEOQRFEbRwcrKwTVkIqR4uxu+e9b483nXX+/8pQhuv7XUW//cZj3c63msA4PHuAwCPRwDAgxQBcHFTdGmj+R+AZiFFAFzNpegeEREdIkWz0uR50Y/Uzqclael8jpQg1+IJXnL0PmfZypCQji2Mce9HZx6P2mfmbfG8mf7K6S00RdtERLSOFNVCk+eRcp5vZHOSOUeNis8sQYZIqXgncsyJpkT6GoV9TqhqK0MqfnmjyJPpJtkolD8tJjgS+xcvOkX6q++ufnDn8vu3sz9XP7jzycP9iu2RosZUpUiTMqQVlZ+1+bH/NYqkTj/4neeuMqT1mH1WH2VhqzTP+R+hiEy0mpJkiEhHsUmONvprJzqSZlJ05boe/vU3Eb158+bP4R+vBv3+ycvj48MrG1VHcukmHWV+PQqLCVk/JCLavhc9SKzbdU7jzU+pu0GXNqgblvZmX+KUuveIiNY3crs6yqy/WqprUZm056skIlKKjCKhxm0zPzYhthbptApZY1I0kWZSdPn920R0/tefr1/9/tvpr/97efzz8X+PfvjWLh8lWxNunBIR3bjpTpG7Fp1SN7uOc8M4RTZvN0KiJGCoRZOeV5m2ExEpEUfIkKzIkp7LaZhtvNlj0CptbVKcoiQ2y5Wi4etXg99O/v/rTy9/+vH4x/3Dg6cHzx+NTVE3Xw2mS5GrXtmV0xXicrc+ol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