qum*_*uma 11 string-formatting utf special-characters mongodb spring-data
我在MongoDB中插入了一个init文件:
db.User.insert({ "_id" : ObjectId("5589929b887dc1fdb501cdba"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "DI.", ... "address" : { "_id" : null, ... "country" : "Österreich" }})
Run Code Online (Sandbox Code Playgroud)
如果我用db.User.find()调用此条目,那么我得到以下内容:
{ "_id" : ObjectId("5589929b887dc1fdb501cdba"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "DI.", ... "address" : { "_id" : null, ... "country" : "�sterreich" } }
Run Code Online (Sandbox Code Playgroud)
带有特殊字符的单词"�sterreich不正确.
有没有人知道我在mongodb可以做些什么才能解决这个问题?
JSON和BSON只能对有效的UTF-8字符串进行编码/解码,如果您的数据(包含的输入)不是UTF-8,您需要在将其传递给任何JSON相关系统之前对其进行转换,如下所示:
$string = iconv('UTF-8', 'UTF-8//IGNORE', $string); // or
$string = iconv('UTF-8', 'UTF-8//TRANSLIT', $string); // or even
$string = iconv('UTF-8', 'UTF-8//TRANSLIT//IGNORE', $string); // not sure how this behaves
Run Code Online (Sandbox Code Playgroud)
我个人更喜欢第一个选项,请参阅iconv()手册页.其他选择包括:
mb_convert_encoding("Österreich","UTF-8","ISO-8859-1");
utf8_encode(utf8_decode($string))您应该始终确保您的字符串是UTF-8编码的,甚至是用户提交的字符串.
猜猜你可以在字符串中使用 HTML 代码
\n\n代码:
\n\n您可以使用ö将 spl 字符保存在数据库中。
\n\ndb.User.insert({ "_id" : ObjectId("5589929b887dc1fdb501cdba"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "DI.", ... "address" : { "_id" : null, ... "country" : "österreich" }})\nRun Code Online (Sandbox Code Playgroud)\n\n使用 db.User.find() 调用此条目时,您将得到以下内容:
\n\n{ "_id" : ObjectId("5589929b887dc1fdb501cdba"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "DI.", ... "address" : { "_id" : null, ... "country" : "\xc3\x96sterreich" } }\nRun Code Online (Sandbox Code Playgroud)\n\n参考:
\n\nhttp://www.starr.net/is/type/htmlcodes.html
\n\n\n\n希望这可以帮助。
\n| 归档时间: |
|
| 查看次数: |
2398 次 |
| 最近记录: |