简体   繁体   中英

How to get a wstring in Chinese from rapidjson::Document?

I'm a student, and developing a PC-Client with cpp. I do not know how to deal with which rapidjson with encoding Unicode. I always get a messy code. I am a jackeroo about cpp, how can i get the correct result? I will appreciate it much!

Just show a example:

class Test {
    // I have got the string of json
    // eg: { "name" : "小明" }
    public : void test(const std::string& data) {
        rapidjson::Document json;
        json.Parse<0>(data.c_str());

        // there are a method GetString() , return a string
        // The name value are another Chinese characters(I guess which because of its encoding).
        // I want to get a wstring which value is "小明"(Not a messy code). How can i do ? 
        std::string name = json["name"].GetString();
    }
};


// I had used this method
// But still got a messy code
str::UnicodeToAnsi();
#include <codecvt>
#include <string>

// convert UTF-8 string to wstring
std::wstring utf8_to_wstring (const std::string& str)
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
    return myconv.from_bytes(str);
}

// convert wstring to UTF-8 string
std::string wstring_to_utf8 (const std::wstring& str)
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
    return myconv.to_bytes(str);
}

Convert wstring to string encoded in UTF-8 Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM