MongoDB特殊字符

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可以做些什么才能解决这个问题?

Som*_*luk 7

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编码的,甚至是用户提交的字符串.


SUN*_*N K 4

猜猜你可以在字符串中使用 HTML 代码

\n\n

代码

\n\n

您可以使用ö将 spl 字符保存在数据库中。

\n\n
db.User.insert({ "_id" : ObjectId("5589929b887dc1fdb501cdba"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "DI.", ... "address" : { "_id" : null, ... "country" : "österreich" }})\n
Run 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" } }\n
Run Code Online (Sandbox Code Playgroud)\n\n

参考

\n\n

http://www.starr.net/is/type/htmlcodes.html

\n\n

替换javascript中字符串中的多个字符

\n\n

希望这可以帮助。

\n