作者: | 来源:互联网 | 2023-08-25 19:02
Describe the bug
Platform: Win10 x64, VS2019, jsoncpp-master
Language: MFC
There some Chinese characters in the "setting.json" which is encoded using UTF-8. And my MFC project is using Unicode character set, When I use the CharReaderBuilder.parseFromStream() to read the "setting.json", all of the Chinese characters becomes garbled and all English characters are right.
So how should I read the Chinese json file correctly?
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| Json::CharReaderBuilder jsonBuilder;
Json::Value jsonRoot;
std::ifstream ifs;
ifs.open("D://Device.json", std::ios::binary);
jsonBuilder["collectComments"] = FALSE;
JSONCPP_STRING errs;
if (parseFromStream(jsonBuilder, ifs, &jsonRoot, &errs))
{
UINT len = 0;
len = jsonRoot.size();
}
for (UINT i = 0; i {
CString tmp;
tmp = jsonRoot[i]["SoftwwareName"].asCString(); // tmp is garbled, not shows "测试用软件"
} |
The setting.json:
1 2 3 4 5 6 7
| [
{
"DeviceName":"设备名称",
"SoftwwareName":"测试用软件",
"IsUsed":"yes"
}
] |
Any idea?
该提问来源于开源项目:open-source-parsers/jsoncpp
Not sure if you still care. I encountered the same thing with Japanese characters. And it turns out that was right.
The short answer is: the content of your
variable is correct, but not displayed properly because Visual Studio uses another character set, which has a different character mapping than UTF-8.
Try following the link to check if your
variable can be shown properly with
https://blogs.msmvps.com/gdicanio/2016/11/22/whats-wrong-with-my-utf-8-strings-in-visual-studio/
You are right. Thanks.