JSON对象

  • JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)
  • JSON 是轻量级的文本数据交换格式
  • JSON 独立于语言 *
  • JSON 具有自我描述性,更易理解

有下面几种创建方式:

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"]);

 

posted @ 2017-05-19 17:14  freeah  阅读(244)  评论(0编辑  收藏  举报