使用单个“智能引用”;我的 JSON 数据破坏了 PHP 脚本
我有一个 PHP 脚本,它正在读取客户端提供的一些 JSON 数据。提供的 JSON 数据中有一个“智能引用”。
示例:
{
"title" : "Lorem Ipsum’s Dolar"
}
在我的脚本中,我使用一个小函数来获取 json 数据:
public function getJson($url) {
$filePath = $url;
$fh = fopen($filePath, 'r') or die();
$temp = fread($fh, filesize($filePath));
$temp = utf8_encode($temp);
echo $temp . "<br />";
$json = json_decode($temp);
fclose($fh);
return $json;
}
如果我对数据进行 utf8 编码,当我回显它时,我在引号所在的位置看不到任何内容。如果我不对数据进行 utf8 编码,当我回显它时,我会看到有趣的问号符号
� 关于如何实际看到正确的字符有什么想法吗?
谢谢!
I've got a PHP script that is reading in some JSON data provided by a client. The JSON data provided had a single 'smart quote' in it.
Example:
{
"title" : "Lorem Ipsum’s Dolar"
}
In my script I'm using a small function to get the json data:
public function getJson($url) {
$filePath = $url;
$fh = fopen($filePath, 'r') or die();
$temp = fread($fh, filesize($filePath));
$temp = utf8_encode($temp);
echo $temp . "<br />";
$json = json_decode($temp);
fclose($fh);
return $json;
}
If I utf8 encode the data, when I echo it out I see nothing where the quote should be. If I don't utf8 encode the data, when I echo it out I see the funny question mark symbol �
Any thoughts on how to actually see the proper character??
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题更多的是生成 JSON 文件。在那里你应该用\'转义'
如果你不能修改这部分,你应该用addslashes这样做:
The issue is more on the side, that generates the JSON file. There you should escape the ' by \'
If you can't modify this part, you should do it like this with addslashes:
假设数据全部是 utf8,您可以进行字符串替换吗?
寻找下面的角色?
Can you possibly do a string replace assuming the data is all utf8?
Looking for the characters below?
服务器是否有可能以 windows-1252 等编码发送 json 数据?该代码页具有一些智能代码字符,其中 iso-8859 具有控制字符。您可以尝试使用 iconv("windows-1252", "utf-8", $temp) 而不是
utf8_encode
吗?如果服务器已经发送 utf-8 编码的 json 那就更好了,因为这是 rfc4627 推荐的编码。Is it possibe that the server is sending the json data in an encoding like windows-1252? That codepage has some smart code characters where iso-8859 has control characters. Could you try to use
iconv("windows-1252", "utf-8", $temp)
instead ofutf8_encode
. Even better would be if the server already sends utf-8 encoded json, since that is the recommended encoding per rfc4627.