2016-02-26 44 views
1

我是一名學生,用cpp開發PC客戶端。我不知道如何處理使用Unicode編碼的rapidjson。我總是得到一個混亂的代碼。我是一個關於cpp的jackeroo,我怎麼才能得到正確的結果?我會很感激!如何從rapidjson :: Document獲取中文wstring?

只需出示一個例子:

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(); 
+0

顯示你到目前爲止試過的東西(代碼)。 – Blacktempel

+0

發佈一些你以前試過的代碼。 –

+0

將'std :: string'轉換爲'std :: wstring'。它不是特定於json庫 –

回答

0
#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謝謝!