time 
设为首页】【收藏本站
当前位置: 主页 > JavaScript > 其它JS框架 > 简明json介绍

简明json介绍

时间:2010-01-05 00:17 点击:627次 字体:[ ]




在JS中我们可以直接定义一个对象:

复制代码 代码如下:

var obj={name:"蓝色",age:10};

上面其实就是所谓的JSON了,我引用下别人的复杂点的例子:
复制代码 代码如下:

{ firstName: "Brett", lastName:"McLaughlin", email: "brett@fengfly.com" }

复制代码 代码如下:

{ people: [
{ firstName: "Brett", lastName:"McLaughlin", email: "brett@fengfly.com" },
{ firstName: "Jason", lastName:"Hunter", email: "fengfly.com" },
{ firstName: "Elliotte", lastName:"Harold", emai": "elharo@fengfly.com" }
]}
{ people1: [
{ firstName: "Brett", lastName:"McLaughlin", email: "brett@fengfly.com" },
{ firstName: "Jason", lastName:"Hunter", email: "fengfly.com" },
{ firstName: "Elliotte", lastName:"Harold", emai": "elharo@fengfly.com" }
],
people2: [
{ firstName: "Brett", lastName:"McLaughlin", email: "brett@fengfly.com" },
{ firstName: "Jason", lastName:"Hunter", email: "fengfly.com" },
{ firstName: "Elliotte", lastName:"Harold", emai": "elharo@fengfly.com" }
],
people3: [
{ firstName: "Brett", lastName:"McLaughlin", email: "brett@fengfly.com" },
{ firstName: "Jason", lastName:"Hunter", email: "fengfly.com" },
{ firstName: "Elliotte", lastName:"Harold", emai": "elharo@fengfly.com" }
]
}

2.后台传输进来的满足JSON条件的文本格式化。
因为JSON最大的用途是AJAX应用,因此关键是怎样把文本转换成javascript对象,假设我们请求回的文本为data,那么我们如下解析:
复制代码 代码如下:

data=data.replace(/\n | \r/g,"");
var obj=eval('('+data+')');

因为我们后台输出的时候会把空格和回车符也输出,因此这里要用正则替换掉,然后用eval转换成对象就可以调用了。



本文地址 : http://www.fengfly.com/plus/view-165019-1.html
标签: json
------分隔线----------------------------
最新评论 查看所有评论
发表评论 查看所有评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码: