我有一个GET函数和JSON响应。 这是功能:从“结果”从json响应中获取变量
$.ajax({
contentType: 'application/json',
dataType: 'json',
success: function(msg){
var result = JSON.stringify(msg);
console.log(result);
},
error: error,
type: 'GET',
url: myurl
});
成功响应是这样{"response":{"values":[{"name":"john","sex":"male"}]}}
但是,当我尝试从响应得到“名”,我得到一个错误! 这是错误:
for(var k in result){
console.log(result[k]);
}
显示字母
我尝试这样做:
$.each(result,function(index, value){
console.log(index, value);
});
但总是出现错误。
嗯,你刚才**字符串化访问值**,它现在是一个字符串,而不是一个对象。直接使用'msg'。 – adeneo
而不是讲一个关于你的代码的故事**发布它**。 – Amit
你的反应本身就是一个JSON对象。意思是如果你想迭代键,你只需要'for(var k in msg)' – Bwolfing