我的PHP脚本返回了一个我在AJAX调用中收到的JSON对象。
$arr[0] = $resp;
$arr[1] = $e;
return json_encode($arr);
现在在我的 AJAX 调用中,我试图获取值,但我得到的只是"/"
或"'"
。
我在 AJAX 中这样做。
dd = JSON.stringify(x.responseText); //this is the response from PHP which is correct I have verified.
alert(dd[0]); //supposed to output $arr[0] but it doesn't
我在这里做错了什么吗?
我在SO上看到过这个
我认为JSON.parse解决了你的问题。
JSON.parse() 方法将字符串解析为 JSON,可选 转换解析产生的价值。
例子
JSON.parse('{}'); // {}
JSON.parse('true'); // true
JSON.parse('"foo"'); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null'); // null
使用齐磊参数
JSON.parse('{"p": 5}', function(k, v) {
if (k === '') { return v; } // if topmost value, return it,
return v * 2; // else return v * 2.
}); // { p: 10 }
JSON.parse('{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}', function(k, v) {
console.log(k); // log the current property name, the last is "".
return v; // return the unchanged property value.
});
裁判:
JSON.parse()
JSON 示例 - 字符串中的对象