c++ - 使用Poco::Json解析json内容,其中有中文字符,抛异常JSON Exception: Bad character.
ringa_lee
ringa_lee 2017-04-17 11:31:36
0
1
1181
std::string jsonStr; // 有中文字符

Poco::Json::Parser parser;
 try {
   parser.parse(jsonStr);
   ...
 }
 catch (Poco::Exception& exc)
 {
  std::cerr << exc.displayText() << std::endl;
 }

请问该如何处理中文字符?谢谢!~

//----问题补充--------------
我发现我程序里的std::string都是mutibyte编码的,而Poco处理Json好像需要UTF8编码,所以我写了个转换的函数,先把mutibyte编码的std::string转成std::wstring(这个是不是算UTF16的?),然后再把wstring转成UTF8编码的std::string.
感觉我的处理有些繁琐……

std::string StringHelper::mutiByteToUTF8(std::string mutibyteStr){
    int len = ::MultiByteToWideChar(CP_ACP, 0, mutibyteStr.c_str(), -1, NULL, 0);  
    std::wstring mbwstr;
    if (len != 0){
        std::vector<wchar_t> unicode(len);  
        ::MultiByteToWideChar(CP_ACP, 0, mutibyteStr.c_str(), -1, &unicode[0], len);  
        mbwstr = &unicode[0];
    }
    std::wstring utf16str = mbwstr;
    std::string utf8str;
    Poco::UnicodeConverter::toUTF8(utf16str,utf8str);
    return utf8str;
}
ringa_lee
ringa_lee

ringa_lee

reply all(1)
阿神

The JSON string itself must be 7bit ASCII encoded. If there is Chinese, it must be encoded in "ua1b2". A non-standard extension can put UTF-8 encoded characters directly in it, but many places do not support it.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!