标签:log ring 语法 eval type json json对象 font console
有下面几种创建方式:
1、创建json对象,json对象在花括号中书写,对象可以包含多个名称/值对:
var txt={"name":"zz","age":18,"phone":"123456"}; console.log(typeof txt); //object console.log(txt); console.log(txt.name);//zz console.log(txt[‘age‘]);//18
2、创建json数组
var txt1=[ {"name":"xx","age":20}, {"name":"rr","age":30} ] console.log(typeof txt1) console.log(txt1); console.log(txt1[0]); console.log(txt1[0].name);//xx console.log(txt1[1]["age"]);//30
3、创建包含json语法的javascript字符串
var txt2=‘{"employ":[‘ + ‘{"name":"zxr","age":20},‘ + ‘{"name":"zzxxrr","age":20}‘ + ‘]‘ + ‘}‘;
var txt3=‘{"employ":[{"name":"zxr","age":20},{"name":"zzxxrr","age":20}]}‘;//也可以这么写
console.log(typeof txt2);//string console.log(txt2.employ);//不能直接这么用,需要转化成对象 var obj = eval ("(" + txt2 + ")");//转化 console.log(obj); console.log(obj.employ[1].name); console.log(obj.employ[1]["age"]);
标签:log ring 语法 eval type json json对象 font console
原文地址:http://www.cnblogs.com/zhaixr/p/6879470.html