Json的语法及使用方法

Json的语法及使用方法

Json(JavaScript Object Notation)对象表示标识,是一种轻量级的数据交换语言,比XML更容易解析,独立于语言和平台。

语法规则:

对象用{}保存

数据用键值对保存

数据间由逗号分隔

数组用[]保存

如下所示:

{

id:1,

name:"john",

arr:[1,2,3],

o:{id:101,name:name101,isFine:true,isHave:null},

age:18,

showName:function(){

  alert(this.name);

},

showAge:function(){

     alert(this.age);

}

}

支持数据类型:

数字,字符串,布尔值,数组,对象,null

{

name:"john",

id:1,

istrue:true,

arr:[1,2,3],

o:{id:101,name:"name101",isFine:true,isHave:null},

empty:null

}

声明:

$(function(){ 
 var str = '{

id:1,

name:"john",

arr:[1,2,3],

o:{

          id:101,

          name:"name101",

          isFine:true,

          isHave:null

},

age:18

}';

取值:
 var obj = $.parseJSON(str);
 console.log("age ="+obj.age);

 console.log("name = " + obj.o.name);
});

输出结果:

console:

18    

name101

JSON字符串转为Json对象

JSon.parse(JsonStr);

$.parseJson(JsonStr);

 

原文地址:https://www.cnblogs.com/minshia/p/5936062.html