很早就听说json传递数据非常方便,今天第一次使用感觉确实挺好的。这里简单介绍一下json吧。
JSON或JavacSript Object Notation,已经成为一种流行的替代XML定义结构化数据。下面是一个简单的例子:
{
“name”: “Bob Miller”,
“age”: 27,
“job”: “Programmer”,
“contact”:{“address”: “253 Johnson Road”,“Home_Phone”: “544-6443″,”Cell_Phone”: “563-3566″},
“hoby”[
{"hoby1":"football","hoby2":"eat"},
{"hoby1":"basketball","hoby2":"drink"}
]
}
服务器返回这样格式的字符串,这时我们需要把接收到的字符串转化成对象,最通用的是eval()函数,下面说一下使用方法:
var result=eual(‘(‘+reponseText+’)');//把result转化成对象
alert(result.name);//输出Bob Miller
alert(result.age);//输出27
alert(result.contact.address);//putout:253 Johnson Road
alert(result.hoby[0].hoby1);//putout:fottball
alert(result.hoby[0].hoby2);//putout:eat
alert(result.hoby[1].hoby1);//putout:baskball