我有两个标记为sample.json和sample2.json的json文件。它们都提供了略有不同的数据,但格式应该相似。当我在sample1.json上运行我的脚本时,它可以工作。当我在sample.json上运行我的脚本时,它无法在第141行和第169行中说出无效的foreach参数。
第141行的代码在这里:
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 for BOTH文件(或者我不明白为什么。我从twitter中提取数据所以应该没问题)。
如果您有时间快速查看,我已经链接了json文件。
https://www.dropbox.com/s/3zqwqtbav7ii9if/sample1.json?dl=0
和
https://www.dropbox.com/s/7xh94csike6ucqc/sample.json?dl=0
即使你可以给我一个关于问题可能是什么的提示,而且为什么我的程序可以在sample1.json上运行而不是sample.json,这会有所帮助。
答案 0 :(得分:0)
所以我发现问题是sample1.json实际上是有效的,而sample.json实际上是无效的。问题是 json.parser.online.fr / 通过将 BOTH 标记为" INVALID"来返回错误的回复。不确定特定检查器无法正常工作的原因(我使用它是因为您可以将文件直接上传到他们的站点)。