Skip to content

Cannot read Chinese characters with Json::CharReaderBuilder in Unicode character set #1134

Closed
@longlei

Description

@longlei

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:

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 < jsonRoot.size(); i++)
{
	CString tmp;
	tmp = jsonRoot[i]["SoftwwareName"].asCString(); // tmp is garbled, not shows "测试用软件"
}

The setting.json:

[
    {
        "DeviceName":"设备名称",
        "SoftwwareName":"测试用软件",
        "IsUsed":"yes"
    }
]

Any idea?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions