我有以下的PHP代码:
我希望能够从 JavaScript 中解析这个字符串(例如使用 jQuery 的
我希望能够记录类似以下内容:
$foo = new stdClass();
$foo->test='hello world';
$bar = new stdClass();
$bar->foo = json_encode($foo);
$encoded_string = json_encode($bar);
$encoded_string
包含:
{"foo":"{\"test\":\"hello world\"}"}
我希望能够从 JavaScript 中解析这个字符串(例如使用 jQuery 的
$.parseJSON
方法):var data = $.parseJSON('{"foo":"{\"test\":\"hello world\"}"}');
console.log(data);
我希望能够记录类似以下内容:
Object {foo: '{"test":"hello world"}'}
当我运行它时(使用chromium),会出现“意外的标记t”错误。
如何在Javascript中解析这个JSON字符串?如果有人想尝试,这里是一个小例子。
addslashes
不是将字符串编码以将其存储在另一个盒子中的适当函数。可以简单地调用json_encode(json_encode($foo))
,但真正的根本问题是为什么首先需要这样做。 - zzzzBov