日期:2014-05-16  浏览次数:20334 次

转 JSON 理解

JSON(JavaScript?Object?Notation)?是一种轻量级的数据交换格式,它采用完全独立于语言的文本格式,可 以用来在客户端和服务器端传输数据!JSON对象既可用于AJAX的开发中,也可用一般的J2EE的开发中,用于一次性向后台提交多于一条的记录!(譬如 显示在页面上的table中记录)?
JSON官方网站的介绍:(
www.json.org)?

JSON?(JavaScript?Object?Notation)?is?a?lightweight?data-interchange?format.?It?is?easy?for?humans?to?read?and?write.?It?is?easy?for?machines?to?parse?and?generate.?It?is?based?on?a?subset?of?the?JavaScript?Programming?Language,?Standard?ECMA-262?3rd?Edition?-?December?1999.?JSON?is?a?text?format?that?is?completely?language?independent?but?uses?conventions?that?are?familiar?to?programmers?of?the?C-family?of?languages,?including?C,?C++,?C#,?Java,?JavaScript,?Perl,?Python,?and?many?others.?These?properties?make?JSON?an?ideal?data-interchange?language.?

JSON?is?built?on?two?structures:?


*?A?collection?of?name/value?pairs.?In?various?languages,?this?is?realized?as?an?object,?record,?struct,?dictionary,?hash?table,?keyed?list,?or?associative?array.

*?An?ordered?list?of?values.?In?most?languages,?this?is?realized?as?an?array,?vector,?list,?or?sequence.?



These?are?universal?data?structures.?Virtually?all?modern?programming?languages?support?them?in?one?form?or?another.?It?makes?sense?that?a?data?format?that?is?interchangable?with?programming?languages?also?be?based?on?these?structures.

简单的示例:?
1。var?jsonObj={person1:{name:"jack",age:"12"},?
person2:{name:"kate",age:"23"},?
person3:{name:"jim",age:"14"}?
};?
调用JSON对象的属性,?
1)jsonObj.person1.name?
2)jsonObj["person1"].name?
可以通过for循环的方式调用JSON对象中的每一个子对象?
for(var?p?in?jsonStr){?
//alert(typeof?p);?alert(p);//返回的是string?
str+=jsonObj[p].name+","+jsonObj[p].age+"<br>";?
}?

2。var?jsonObj2={persons:[{name:"jordan",sex:"m",age:"40"},?
{name:"bryant",sex:"m",age:"28"},?
{name:"McGrady",sex:"m",age:"27"}?
]};?

调用JSON对象的属性,?
1)jsonObj2.persons[0].name;?
2)for循环调用方法,?
var?persons=jsonObj2.persons;//返回的值是一个数组object?
for(var?i=0;i<persons.length;i++){?
cur_person=persons[i];?
str+=cur_person.name+"'sex?is?"+cur_person.sex+"?and?age?is?"+cur_person.age+"<br>";?
}?
向jsonObj2的persons数组中插入,删除,更新?数据,?
此处,可以利用javascript的Array对象的方法进行操作,如?
var?person={name:"yaoMing",sex:"m",age:"26"};?
jsonObj2.persons.push(person);//数组最后加一条记录?
jsonObj2.persons.pop();//删除最后一项?
jsonObj2.persons.shift();//删除第一项?
jsonObj2.persons.unshift(person);//数组最前面加一条记录?
只要适合Javascript的方法都是可以用在JSON对象的数组中的!所以还有另外的方法splice(?)进行crud操作!?
//删除?
jsonObj2.persons.splice(0,1);//开始位置,删除个数?
//替换不删除?
var?self={name:"tom",sex:"m",age:"24"};?
var?brother={name:"Mike",sex:"m",age:"29"};?
jsonObj2.persons.splice(1,0,self,brother);//开始位置,删除个数,插入对象?
//替换并删除?
var?self={name:"tom",sex:"m",age:"24"};?
var?brother={name:"Mike",sex:"m",age:"29"};?
jsonObj2.persons.splice(0,1,self,brother);//开始位置,删除个数,插入对象?

二,知道了在Javascript中JSON的基本操作,但这还不能与后台进行交互,在着之前还需要把它转化为?String!?

方法有两个,1)引入一个方法?:?

function?obj2str(o){?
var?r?=?[];?
if(typeof?o?=="string")?return?"/""+o.replace(/([/'/"//])/g,"//$1").replace(/(/n)/g,"//n").replace(/(/r)/g,"//r").replace(/(/t)/g,"//t")+"/"";?
if(typeof?o?==?"object"){?
if(!o.sort){?
r[0]="{"?
for(var?i?in?o){?
r[r.length]=i;?
r[r.length]=":";?
r[r.length]=obj2str(o[i]);?
r[r.length]=",";?
}?
if(!!document.all?&&?!/^/n?function/s*toString/(/)/s*/{/n?/s*/[native?code/]/n?/s*/}/n?/s*$/.test(o.toString)){?
r[r.length]="toString:"+o.toString.toString();?
r[r.length]=",";?
}?
r[r.length-1]="}"?
}else{?
r[0]="["?
for(var?i?=0;i<o.length;i++){?
r[r.length]=obj2str(o[i]);?
r[r.length]=",";?
}?
r[r.length-1]="]"?
}?
return?r.join("");?
}?