毕晓峰*_*毕晓峰 2 c++ winapi rapidjson
我是一名学生,正在使用 cpp 开发 PC 客户端。我不知道如何处理使用Unicode编码的rapidjson。我总是得到一个凌乱的代码。我是一个关于 cpp 的 jackeroo,我怎样才能得到正确的结果?我会很感激的!
举个例子:
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();
Run Code Online (Sandbox Code Playgroud)
#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);
}
Run Code Online (Sandbox Code Playgroud)
将 wstring 转换为以 UTF-8 编码的字符串谢谢!