日期:2014-05-16 浏览次数:20409 次
var myJSONObject = {"bindings": [ {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}, {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"}, {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"} ] };
?这个例子中,创建了一个对象,它只包含一个成员“bindings”。“bindings”是一个包含了3个对象的数组,而这每个对象都有"ircEvent"、"method"和"regex"3个成员。
这些成员可以用“.”或subscript 操作得到。
如:
myJSONObject.bindings[0].method // "newURI" myJSONObject.bindings[1].deleteURI // "newURI"
?//声明字符串,可对比一下json文本与我们正常文本的区别
var normalstring='[{persons:[{name:"jordan",sex:"m",age:"40"}, {name:"bryant",sex:"m",age:"28"}, {name:"McGrady",sex:"m",age:"27"} ]}]'; var jsontext='[{"persons":[{"name":"jordan","sex":"m","age":"40"}, {"name":"bryant","sex":"m","age":"28"}, {"name":"McGrady","sex":"m","age":"27"} ]}]';
?我们可以使用eval()函数调用JavaScript的编译器把JSON文本转变成对象。因为 JSON是JavaScript的一个确切的子集,编译器可以正确地解析JSON文本,然后生成一个对象结构。
//调用eval函数转换为json对象, var myE = eval(normalstring); //将json对象转换为字符串 var text = JSON.stringify(myE); //对比转换后的json文本与声明的文本区别 document.writeln('转换后的json文本:'+text+'<br><br>声明的json格式文本 '+jsontext+'<br><br>声明的普通格式文本 '+normalstring+'<br><br>');
?结果如下:
转换后的json文本:[{"persons": [{"name":"jordan","sex":"m","age":"40"},{"name":"bryant","sex":"m","age":"28"},{"name":"McGrady","sex":"m","age":"27"}]}]
声明的json格式文本 [{"persons":[{"name":"jordan","sex":"m","age":"40"},{"name":"bryant","sex":"m","age":"28"},{"name":"McGrady","sex":"m","age":"27"}]}]
声明的普通格式文本 [{persons:[{name:"jordan",sex:"m",age:"40"}, {name:"bryant",sex:"m",age:"28"}, {name:"McGrady",sex:"m",age:"27"} ]}]
小结:转换后的json文本和声明的json格式文本内容是相同的。
//当安全比较重要的时候使用JSON解析就好一些。JSON解析只会识别JSON文本并且它更安全,下面调用json的parse函数对文本数据转换生成json数据结构
var myData = JSON.parse(jsontext);
?完整的文件如下(区别:myJSONObject,jsontext,normalstring的不同):
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript" src="js/json2.js"></script> </head> <body> <script> var normalstring='[{persons:[{name:"jordan",sex:"m",age:"40"}, {name:"bryant",sex:"m",age:"28"}, {name:"McGrady",sex:"m",age:"27"} ]}]'; var jsontext='[{"persons":[{"name":"jordan","sex":"m","age":"40"}, {"name":"bryant","sex":"m","age":"28"}, {"name":"McGrady","sex":"m","age":"27"} ]}]'; var myJSONObject = {"bindings": [ {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}, {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"}, {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"} ] }; //调用eval函数转换为json对象, var myE = eval(normalstring); //将json对象转换为字符串 var text = JSON.stringify(myE); //对比转换后的json文本与声明的文本区别 document.writeln('转换后的json文本:'+text+'<br><br>声明的json格式文本 '+jsontext+'<br><br>声明的普通格式文本 '+normalstring+'<br><br>'); //JSON解析 var myData = JSON.parse(jsontext); </script> </body> </html>
?4.)示例演示二:
//下面是对json对象的增删查改操作 <%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <script type="text/javascript" src="js/json2.js"></script> </head> <body> <script> //声明