Javascript 无效的foreach参数
我有两个json文件,分别标记为sample.json和sample2.json。他们两人提取的数据略有不同,但格式应该相似。当我在sample1.json上运行脚本时,它可以工作。当我在sample.json上运行脚本时,第141行和第169行的foreach参数无效 第141行代码如下:Javascript 无效的foreach参数,javascript,php,json,parsing,encode,Javascript,Php,Json,Parsing,Encode,我有两个json文件,分别标记为sample.json和sample2.json。他们两人提取的数据略有不同,但格式应该相似。当我在sample1.json上运行脚本时,它可以工作。当我在sample.json上运行脚本时,第141行和第169行的foreach参数无效 第141行代码如下: foreach($posts as &$entry){ $mod = new modifier($entry); //create modifier object
foreach($posts as &$entry){
$mod = new modifier($entry); //create modifier object
$result = $mod->getResult(); //gives parsed text back
$entry['mod-post'] = $result;
$usr = $entry['user']['screen_name']; //mod the usr as well while we are at it to give a new usr
$usr = '@' . $usr;
$entry['mod-usr'] = $usr;
}
当我读到里面的数据时
$datafile = 'data/sample.json';
//open up datafile created through search
$myfile = fopen($datafile, 'r') or die('Unable to open file!!!');
$d = fread($myfile, filesize($datafile));
//file already formatted as json
$data = array();
$data = json_decode($d, true);
$posts = $data["statuses"]; //make easier to access later
fclose($myfile);//close file
根据我所看到的,我认为问题在于第二个json文件没有被正确读取。也就是说,当我将文件上传到jsonlint或任何json检查器时,这两个文件的json都无效,或者我不知道为什么。我正在从twitter上获取数据,所以应该没问题
如果您有时间快速查看,我已经链接了json文件
及
即使您能给我一个提示,说明问题可能是什么,以及为什么我的程序可以在sample1.json上运行而不能在sample.json上运行,这也会很有帮助。所以我发现问题在于sample1.json实际上是有效的,sample.json实际上是无效的。问题是json.parser.online.fr/返回了一个不正确的响应,将它们都标记为无效。不确定为什么那个特定的检查器不工作,我使用它是因为你可以直接上传文件到他们的网站 更新:我一直在检查json文件,json Lint给了sample1一个有效的响应。似乎json.parser.online.fr/不起作用!!!我会继续调查!