首页 > 其他分享 >libcurl 坑之 CURLOPT_WRITEFUNCTION

libcurl 坑之 CURLOPT_WRITEFUNCTION

时间:2023-02-16 17:22:44浏览次数:40  
标签:std return source libcurl wstring WRITEFUNCTION 序列化 CURLOPT

参考blog:

https://blog.csdn.net/szchtx/article/details/21740599

 

 

 CURLOPT_WRITEFUNCTION 回调回来的数据始终是不规则的json。

服务器返回的是utf-8,使用win32接口utf-8 To ansi (MS官网手册就有)

                                    使用c++11(下面附上)

#include <locale>
#include <codecvt>


int strTowstr(std::wstring& ws, const std::string& s)
{
  std::wstring wsTmp(s.begin(), s.end());

  ws = wsTmp;

  return 0;
}

std::string utf8_encode(const std::wstring& source)
{
  return std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(source);
}

std::wstring utf8_decode(const std::string& source)
{
  return std::wstring_convert<std::codecvt_utf8<wchar_t>>().from_bytes(source);
}

两者转换接口一样,此时看了上面老哥的的说法,我验证了一下可能我取的方法有问题,直接拷贝一份,并且附上结束符就解决了

错误的数据: json 0尾部多了个0导致不能正常序列化反序列化

通过拷贝出来自己附上结束符: json并且能正常的序列化,反序列化

 

标签:std,return,source,libcurl,wstring,WRITEFUNCTION,序列化,CURLOPT
From: https://www.cnblogs.com/liuruoqian/p/17127473.html

相关文章