How can I deal with this encoding?
I'm now trying to parse chrome bookmarks, but 开发者_运维技巧I encounter a problem. the bookmarks snippet is presented as follow:
{
"date_added": "12915566290018721",
"id": "16",
"name": "hao123\uFF0D\uFF0D\u6211\u7684\u4E0A\u7F51\u4E3B\u9875",
"type": "url",
"url": "http://www.hao123.com/"
}
the string coding corresponding to name field is stored as "hao123\uFF0D\uFF0D\u6211\u7684\u4E0A\u7F51\u4E3B\u9875", but it should be "hao123--我的上网主页" to provide to users. How can I transform "hao123\uFF0D\uFF0D\u6211\u7684\u4E0A\u7F51\u4E3B\u9875" to "hao123--我的上网主页"?
thanks!
You can try this code
std::string name = root.get("name","").asString();
USES_CONVERSION;
std::wstring wstr_name(A2W_CP(name.c_str(), CP_UTF8));
What you're looking at are UTF-16 code points in the string. Unless you have a JSON library that handles Unicode for you, consider iterating the string and looking for the escape sequence that denotes the UTF-16 code point "\u". From there you can transform the string to whatever encoding is necessary for it to output properly.
Thanks codeka, I solve the problem.
std::string name = root.get("name","").asString();
cout<<name<<endl;
int len=strlen(name.c_str())+1;
WCHAR outName[MAX_PATH];
// MultiByteToWideChar(CP_UTF8, 0, name.c_str(), len, outName, len);
char outch[MAX_PATH];
WCHAR * wChar=new WCHAR[len];
wChar[0]=0;
MultiByteToWideChar(CP_UTF8, 0, name.c_str(), len, wChar, len);
WideCharToMultiByte(CP_ACP, 0, wChar, len, outch , len, 0, 0);
delete [] wChar;
cout<<outch<<endl;
Thanks codeka & fbrereto again.
As far as I can tell from looking at the Jsoncpp source code, it looks like it should decode the string properly and you'll get a UTF-8 string back. If that is not what you're seeing, please post the code that you're actually using, and what you're getting back instead.
Up is work. here is my code。
Json::Reader reader;
Json::Value root;
if (reader.parse(response, root))
{
if (root.isMember("data")&& root.isMember("msg"))
{
Json::Value data = root["data"];
std::string str = root["msg"].asCString();
std::string cstr = UnicodeToGB2312(str);
AndroidSay("%s", cstr.c_str());
}
}
string UnicodeToGB2312(std::string uData)
{
int len = strlen(uData.c_str()) + 1;
char outch[MAX_PATH];
WCHAR * wChar = new WCHAR[len];
wChar[0] = 0;
MultiByteToWideChar(CP_UTF8, 0, uData.c_str(), len, wChar, len);
WideCharToMultiByte(CP_ACP, 0, wChar, len, outch, len, 0, 0);
delete[] wChar;
char* pchar = (char*)outch;
return pchar;
}
精彩评论