这是我的PHP数组:
$entries = array( 1420934400 => array( 'entry' => 'I think I liked it.', 'data' => 'some' ), 1452470400 => array( 'entry' => 'Turkey is much better. Tastes more like chicken.', 'data' => 'no calls' ));
然后我转换为JSON
$entries = json_encode($entries);
这会产生字符串:
{"1420934400":{"entry":"I think I liked it.","data":"some"},"1452470400":{"entry":"Turkey is much better. Tastes more like chicken.","data":"no calls"}}
...我相信它是有效的JSON.但是当我尝试使用JavaScript访问时:
我收到以下错误:
SyntaxError:JSON.parse:JSON数据的第1行第2列的意外字符
谁能看到我哪里出错了?
你不需要JS中的JSON.parse,因为JSON可以直接由JS解释(因为某种原因,它被称为JavaScript Object Notation ;-).做
var fetchedEntries = ;
当您以字符串形式接收JSON数据时,JSON.parse是合适的.例如,这也适用:
var fetchedEntries = JSON.parse( "" );