Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何防止JSON字符串中出现\r\n和类似内容_Mysql_Laravel 4_Json - Fatal编程技术网

Mysql 如何防止JSON字符串中出现\r\n和类似内容

Mysql 如何防止JSON字符串中出现\r\n和类似内容,mysql,laravel-4,json,Mysql,Laravel 4,Json,我通过laravel模型从MySQL获取文本,并将内容转换为JSON。不幸的是,文本包含新行和回车符,有没有办法使内容正确转义 Article::find(1)->toJSON(); 在我的视图中给出了一个Ajax/JSON错误,所以我想知道问题出在哪里。我是以错误的方式存储内容还是以错误的方式检索内容 谢谢 这是我为一篇测试文章获得的JSON字符串: {"id":22,"short_title":"Another test article","long_title":"Longer t

我通过laravel模型从MySQL获取文本,并将内容转换为JSON。不幸的是,文本包含新行和回车符,有没有办法使内容正确转义

Article::find(1)->toJSON();
在我的视图中给出了一个Ajax/JSON错误,所以我想知道问题出在哪里。我是以错误的方式存储内容还是以错误的方式检索内容

谢谢

这是我为一篇测试文章获得的JSON字符串:

{"id":22,"short_title":"Another test article","long_title":"Longer title for the test      article mention in the short title","description":"This article describes a computer classroom where strange things happen and stuf","content":"This is a test article and I think this will generate the error I am looking for.    <\/p>\r\n\r\n Maybe or maybe not. <\/p>\r\n","deleted_at":null,"created_at":"2014-04-25  09:10:45","updated_at":"2014-04-25  09:10:45","category_id":1,"zenra_link":"","published_on":null,"published":1,"source_url":"http:\/\/blog.livedoor.jp\/morisitakurumi\/archives\/51856280.html","source_title":"Some source article","source_date":null,"slug":"another-test-article","view_count":0}
{“id”:22,“短标题”:“另一篇测试文章”,“长标题”:“短标题中提到的测试文章的长标题”,“描述”:“这篇文章描述了一个发生奇怪事情和stuf的计算机教室”,“内容”:“这是一篇测试文章,我想这会产生我正在寻找的错误。\r\n\r\n可能会,也可能不会。\r\n”、“删除位置”:null、“创建位置”:“2014-04-25 09:10:45”、“更新位置”:“2014-04-25 09:10:45”、“类别id”:1,“zenra\u链接”:““发布位置”:null,“发布位置”:1,“源地址”:”http:\/\/blog.livedoor.jp\/morisitakurumi\/archives\/51856280.html,“源标题”:“某些源文章”,“源日期”:null,“slug”:“另一篇测试文章”,“查看计数”:0}
内容部分由运行CKEditor的textarea生成,并保存到MySQL MEDIUMTEXT字段中

我要做的下一件事是将其注入到我的视图中,以填充我的主干视图,如下所示:

<script>var initialData = JSON.parse('{{ $cJson }}');</script>
<script>var initialData = {{ $cJson }};</script>
var initialData=JSON.parse('{{$cJson}}');

这就是控制台告诉我的:Uncaught SyntaxError:Unexpected Token。正如建议的那样,我在jsonlint.com上测试了上面的字符串,结果显示它是有效的。

我发现了问题:

<script>var initialData = JSON.parse('{{ $cJson }}');</script>
var initialData=JSON.parse('{{$cJson}}');
这就是错误发生的地方。JSON.parse会在我提供的字符串上出错。如果我删除JSON.parse,它的工作原理如下:

<script>var initialData = JSON.parse('{{ $cJson }}');</script>
<script>var initialData = {{ $cJson }};</script>
var initialData={{{$cJson};

没有更多意外的令牌错误。我在错误的轨道上遇到了这个问题,因为我读了太多的地方,以至于回车和新行都会产生JSON格式的错误。

不知道这是否重要,但是
toJSON
应该是
toJSON
-事实上不是这样,代码片段更多的是为了演示我是如何得到的我的JSON字符串,而不是显示实际代码。您的代码段看起来不错(使用
->toJson()
)。您到底收到了什么样的错误?我通过Ajax检索文本内容后得到了“意外标记”,这是因为字符串中出现了\n\r次。”这是因为字符串中出现了\n\r次。“你怎么知道的?您是否已获取返回的字符串并在jsonlint.com上对其进行了测试?