JSON 特殊字符 JSON php 5.2.13
我对这些编码问题感到疯狂......
我使用 json_decode 和 json_encode 来存储和检索数据。我发现,json 总是需要 utf-8。那里没问题。我用 utf-8 给出 json 'hello',在我的数据库中它看起来像 hello00f6
。好的,代码点。但是当我使用 json_decode
时,它不会将代码点解码回来,所以我仍然有 hello00f6
。 另外,在 php 5.2.13 中,JSON 中似乎仍然没有可选标签。如何将代码点字符转换回正确的特殊字符以在浏览器中显示?
问候并感
谢梅尼
I'm getting crazy over these encoding probs...
I use json_decode
and json_encode
to store and retrieve data. What I did find out is, that json always needs utf-8. No problem there. I give json 'hellö' in utf-8, in my DB it looks like hellu00f6
. Ok, codepoint. But when I use json_decode
, it won't decode the codepoint back, so I still have hellu00f6
.
Also, in php 5.2.13 it seems like there are still no optionial tags in JSON. How can I convert the codepoint caracters back to the correct specialcharacter for display in the browser?
Greetz and thanks
Maenny
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能是因为 JSON unicode 字符串中代码点前面的反斜杠:
ö
表示为\u00f6
。当存储在数据库中时,DBMS 不知道如何解释\u00f6
因此我猜它会将其读取(并存储)为u00f6
。您使用转义函数吗?
尝试在 unicode 转义字符上添加反斜杠:
It could be because of the backslash preceding the codepoint in the JSON unicode string:
ö
is represented\u00f6
. When stored in your DB, the DBMS doesn't knows how to interpret\u00f6
so I guess it reads (and store) it asu00f6
.Are you using an escaping function ?
Try adding a backslash on unicode-escaped chars:
前面的文章已经解释了为什么您的示例没有按预期工作。
然而,在使用数据库时有一些好的编码实践,这对于提高应用程序的安全性(即防止 SQL 注入)非常重要。
以下示例旨在展示其中一些实践,并假设 PHP 5.2 和 MySQL 5.1。 (请注意,所有文件和数据库条目均使用 UTF-8 编码存储。)
此示例中使用的数据库名为
test
,表的创建如下:(请注意,编码设置为
utf8_bin
。)它遵循 php 代码,用于添加新条目和创建 JSON:
请注意方法
$conn->quote(..) 在将数据传递到数据库之前。正如上一篇文章中提到的,使用准备好的语句会更好,因为它们已经完成了整个转义。因此,如果我们写:
而不是
结论: 对所有存储或传输给用户的字符数据使用 UTF-8 是合理的。它使国际化 Web 应用程序的开发变得更加容易。为了确保用户输入正确发送到数据库,使用转义函数是一个好主意。否则,使用准备好的语句可以使生活和开发变得更加容易,并且可以进一步提高应用程序的安全性,因为可以防止 SQL 注入。
The preceding post already explains, why your example did not work as expected.
However, there are some good coding practices when working with databases, which are important to improve the security of your application (i.e. prevent SQL-injection).
The following example intends to show some of these practices, and assumes PHP 5.2 and MySQL 5.1. (Note that all files and database entries are stored using UTF-8 encoding.)
The database used in this example is called
test
, and the table was created as follows:(Note that the encoding is set to
utf8_bin
.)It follows the php code, which is used for both, adding new entries and creating JSON:
Note the usage of the method
$conn->quote(..)
before passing data to the database. As mentioned in the preceding post, it would even be better to use prepared statements, since they already do the whole escaping. Thus, it would be better if we write:instead of
Conclusion: Using UTF-8 for all character data stored or transmitted to the user is reasonable. It makes the development of internationalized web applications way easier. To make sure, user-input is properly sent to the database, using an escape function is a good idea. Otherwise, using prepared statements make life and development even easier and furthermore improves your applications security, since SQL-Injection is prevented.