Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support the Chinese language in json string #694

Closed
yuanhui2015 opened this issue Aug 11, 2017 · 28 comments
Closed

support the Chinese language in json string #694

yuanhui2015 opened this issue Aug 11, 2017 · 28 comments

Comments

@yuanhui2015
Copy link

new version doesn't support the Chinese language in json string, I has been fixed. please see the attachment.

@nlohmann
Copy link
Owner

There is no attachment.

The library does support UTF-8, so Chinese characters should not be a problem. Can you provide an example?

@yuanhui2015
Copy link
Author

{"authority":0,"category":6,"category_id":"0","delete_by":"","delete_time":"","ext":"","ext_link":0,"is_delete":0,"is_file":0,"is_share":0,"lv_creator":"","lv_id":1,"lv_size":0,"lv_time":"","name":"回收站","x_id":6,"x_pid":1}

@nlohmann
Copy link
Owner

What is the error message and how does the code look like that produces this error message?

@yuanhui2015
Copy link
Author

error occured in the function get_token() when parse the Chinese string

@yuanhui2015
Copy link
Author

can you find my attachment in this topic, I didn't find it.

@nlohmann
Copy link
Owner

This code works for me:

#include <json.hpp>
#include <iostream>
using json = nlohmann::json;
 
int main()
{
    std::string s = R"({"authority":1,"category":6,"category_id":"0","delete_by":"","delete_time":"","ext":"","ext_link":0,"is_delete":0,"is_file":0,"is_share":0,"lv_creator":"","lv_id":1,"lv_size":0,"lv_time":"","name":"回收站","x_id":6,"x_pid":1})";
    json j = json::parse(s);
    std::cout << std::setw(2) << j << std::endl;
}

What is the error message and how does the code look like that produces this error message?

@yuanhui2015
Copy link
Author

test code is as below:
CString strText;
m_oJsonView.GetWindowTextW(strText);
std::string szJson = ToSTR(strText);
json j3 = json::parse(szJson);

@nlohmann
Copy link
Owner

Can you make sure the content of szJson is exactly the one from #694 (comment) ?

What is the error message?

@nlohmann
Copy link
Owner

(You seem to copy the string from window text into a CString variable. Maybe the string is encoded as UTF-16 or UTF-32? The library only supports UTF-8.)

@josephshen
Copy link

GetWindowTextW do not mean you get a utf8 encode string!!
@nlohmann it's his problem, don't waste time here

@yuanhui2015
Copy link
Author

error message is:parse error - unexpected '"' on line 11615.
I used VS2015 and the charset I used is unicode.

@nlohmann
Copy link
Owner

Which Unicode? The library only supports UTF-8.

@yuanhui2015
Copy link
Author

@josephsmeng I convert CString to std::string by my funciton ToSTR, do you see it?
std::string ToSTR(CString cs)
{
std::string sstring = "";
if(cs.GetLength() == 0)
return sstring;

BSTR bstr = cs.AllocSysString();
_bstr_t bstr_t(bstr);	
sstring.append(bstr_t);
SysFreeString(bstr);

return sstring;

}

@josephshen
Copy link

@nlohmann you are just so nice, let the young man google his problem, and lean some thing about Unicode ....

@yuanhui2015
Copy link
Author

when I create a new project by VS2015, the default charset is Unicode. you can find it in VS as below
solution->property->config property->normal->charset

@nlohmann
Copy link
Owner

"Unicode" describes a family of encodings. For this library, the string you pass to parse needs to be UTF-8 encoded. UTF-16 or UTF-32 are not supported.

@yuanhui2015
Copy link
Author

got it, thank you nlohmann.
you are great~~

@josephshen
Copy link

你可以从这里开始 http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html,
你的问题其实只需要把UTF-16编码的字符串转换成UTF-8的,
我只告诉你到这里,你需要再自己学习一下字符串编码相关的知识,这是很基础的问题。

@yuanhui2015
Copy link
Author

呵呵,我不喜欢在编码上浪费时间。
我只修改了json.hpp几行代码,就完全可以支持中文了,你觉得还有必要转换编码么?

@josephshen
Copy link

你高兴就好啦😄😄😄

@JueLance
Copy link

JueLance commented Mar 22, 2018

@yuanhui2015

when I create a new project by VS2015, the default charset is Unicode. you can find it in VS as below
solution->property->config property->normal->charset

this setting is not meaning your program character is UTF-8. It means the windows library function will use Unicode version funciton, such as SetWindowTextA() and SetWindowTextW(). When you use "Use Unicode Character Set", the unicode version function - SetWindowTextW will be call finally, not SetWindowTextA.

@smily1223
Copy link

呵呵,我不喜欢在编码上浪费时间。 我只修改了json.hpp几行代码,就完全可以支持中文了,你觉得还有必要转换编码么?

老哥怎么解决中文的问题的?能说一下吗?

@JueLance
Copy link

呵呵,我不喜欢在编码上浪费时间。 我只修改了json.hpp几行代码,就完全可以支持中文了,你觉得还有必要转换编码么?

老哥怎么解决中文的问题的?能说一下吗?

不建议直接改代码,可以看看我之前在类似问题的解决方向:#1022

@smily1223
Copy link

好的,谢谢老哥

@Olvi73
Copy link

Olvi73 commented May 20, 2023

In Qt project, you can use
ui->lineEdit->text().toUtf8().data() to get a value
and use
ui->textEdit->setText(QString::fromUtf8(j.dump(4).c_str()));
to set this text

@wuhaitao93
Copy link

1、确保windows中源文件(visual studio中)都是utf-8 with bom格式;
2、确保vs---项目属性---c/c++---命令行 :其他选项 中有 /utf-8;
至此,json可实现正常解析,保存到文件不会乱码,但是打印到控制台仍然乱码,是因为控制台默认编码方式是936 GBK,要想解决,有两种方法:
a、写一个U2G函数,把UTF-8转换为GBK,打印到控制台不乱码;
b、https://blog.csdn.net/weixin_45265547/article/details/121931397 参考这个,把控制台默认编码方式改为UTF-8,这种方式不建议,因为windows上默认都是GBK输出,改了后其他工程可能会乱码;

综上,nlohmann在源码中应该考虑windows环境下输出到控制台时做U2G转换,不过考虑开源不易,期待有朋友提pr改一下~

@nlohmann
Copy link
Owner

nlohmann commented Apr 7, 2024

The library only supports UTF-8.

@wuhaitao93
Copy link

The library only supports UTF-8.

Yes, so let the users solve other encoding problems themselves ^.^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants