rapidjson 中文乱码的问题

tony2278 2018-07-26 10:39:09


rapidjson::Document doc_config;
int ret = GetDocumentFromJsonFile(path, doc_config);
const rapidjson::Value& myValue = doc_config["myKey"];
std::string data = testing["mySubkey"].GetString(); //here

//data 如果存在中文就会乱码,应该怎样转换处理呢?谢谢!

...全文
1742 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
json是utf8需要转换成unicode或者ansi使用这个转换:

wstring StringHelper::UTF8ToUnicode( const string & s)
{
wstring result;

int n = MultiByteToWideChar( CP_UTF8, 0, s.c_str(), -1, NULL, 0 );
wchar_t * buffer = new wchar_t[n];

::MultiByteToWideChar( CP_UTF8, 0, s.c_str(), -1, buffer, n );

result = buffer;
delete[] buffer;

return result;
}


std::string WChar2Ansi(LPCWSTR pwszSrc)
{
int nLen = 0;
nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
if (nLen<= 0)
return std::string("");

char* pszDst = new char[nLen];
if (NULL == pszDst)
return std::string("");

WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
pszDst[nLen -1] = 0;
std::string strTemp(pszDst);
delete [] pszDst;

return strTemp;
}
tony2278 2018-07-30
  • 打赏
  • 举报
回复
UTF-8 <----> GB
(ASCII)

64,632

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧