首页 > 代码库 > javascript操作json
javascript操作json
var jsonStr = ‘{"team": "铁三角", "members": [{"name":"小明", "age": 7},{"name":"小红", "age": 8},{"name":"小刚", "age": 9}]}‘;//字符串
var jsonObj = {"team": "铁三角", "members": [{"name":"小明", "age": 7},{"name":"小红", "age": 8},{"name":"小刚", "age": 9}]};//对象
jsonObj = eval("(" + jsonStr + ")");//字符串向对象转换
//遍历铁三角成员
for (var i = 0; i < jsonObj.members.length; i++) {
alert(jsonObj.members[i].name);
}
//遍历小明的属性
var ming = jsonObj.members[0];
for (var key in ming) {
alert(key);
alert(ming[key]);
}
****************************
ajax请求json的jquery实现:
B:
$.ajax({
url: "a.aspx",
dataType: "json",
type: "post",
success: function(data) {alert(data[0].demo);},
error: function(XMLHttpRequest, status, error) {alert(error);}
});
S:a.aspx
Response.Clear();
Response.Write("[{\"demo\":\"This Is The JSON Data\"}]");
Response.Flush();
Response.End();
javascript操作json