我的ajax结果返回下面的这个Json对象,我想把它变成一个javascript关联数组来遍历它..
{"store_id":"10","company_id":"1","username":"tony","password":"19f060ed6b09502715175c1cdb562df8eec1070c","name":"Dbarpos1","email":"dfbarco@gmail.com","address":"2738 N Bristol St Santa Ana CA 92706 ","phone_number":"7142425639","city":"","state":"","zip":"","goals":"New 40, Up 35, Acc $1,500","return_policy":" 0 returms","drawer_drop":"after_300","drawer_option":"indiv_200","sales_tax":"8.5%","store_schedule":"","store_logo":"","group":"","created_by":"diego barco","status":"active","contact_person":"Diego Castillo"}
答案 0 :(得分:1)
更新您的ajax调用以包含数据类型:
$.ajax({
url:"edit_store_ajax.php",
data:{store: store_id},
type:"POST",
success:function(result){ for (var key in result) { } },
dataType:"json"
});
如果数据类型没有帮助,则需要将String解析为JSON对象。您可以使用jQuery.parseJSON()来帮助:http://api.jquery.com/jquery.parsejson/
$.ajax({
url:"edit_store_ajax.php",
data:{store: store_id},
type:"POST",
success:function(result){ result = $.parseJSON(result); for (var key in result) { } }
});
你可以像关联数组一样循环遍历它......
for (var key in json) {
console.log(key + ":" + json[key]);
}
或者获取单个值:
console.log(json["store_id"]);
console.log(json["username"]);
console.log(json.store_id);
console.log(json.username);
答案 1 :(得分:0)
您可以在较新的浏览器中使用JSON.parse(data)
,如果您支持较旧的浏览器,则可以使用polyfill。