转载:https://blog.csdn.net/foruok/article/details/50687864(解析json)

转载:https://blog.csdn.net/foruok/article/details/51132462(构建json)

在CEF项目开发中避免不了要使用json格式的数据,为了解决对json的使用,一般都是引入第三方开源库JsonCpp、cJson等,问题是解决了;但这样会增加客户端的体积,查阅资料发现CEF的确强大,它支持对json的操作,如果不想引入第三方库,可以使用CEF操作JSON。

Cef提供了JSON解析功能,在cef_parser.h文件内有三个JSON相关的方法:

  • CefParseJSON
  • CefParseJSONAndReturnError
  • CefWriteJSON

1.CEF解析JSON

void CMainFrameWnd::TestParseJson()
{
std::wstring wstrJson = _T("{");
wstrJson += _T("\"uid\" : \"chechenluoyang@163.com\",");
wstrJson += _T("\"fileName\" : \"梅西.txt\",");
wstrJson += _T("\"time\" : \"2017.07.29 10:55:22\",");
wstrJson += _T("\"type\" : \"Libcurl HTTP POST JSON \",");
wstrJson += _T("\"authList\" : \"test\"");
wstrJson += _T("}"); CefRefPtr<CefValue> jsonObject = CefParseJSON(wstrJson, JSON_PARSER_ALLOW_TRAILING_COMMAS); if (jsonObject->IsValid())
{
CefRefPtr<CefDictionaryValue> dict = jsonObject->GetDictionary();
CefString strUid = dict->GetString("uid");//得到chechenluoyang@163.com
CefString strFilename = dict->GetString("fileName");//得到梅西.txt
}
}

注:因为我的工程属性设置的是Unicode编码,所以在用字符串拼接的时候用wstring来存储,不然中文会乱码。

2.构建JSON

void CMainFrameWnd::TestWriteJson()
{
CefRefPtr<CefDictionaryValue> pDict = CefDictionaryValue::Create(); pDict->SetInt("id", );
pDict->SetString("name", _T("梅西")); CefRefPtr<CefValue> pValue = CefValue::Create(); pValue->SetDictionary(pDict);
std::wstring strJson = CefWriteJSON(pValue, JSON_WRITER_DEFAULT);
}

生成的JSON串:

{
"id":9527,
"name":"梅西"
}

注:因为Unicode编码,json的key value都是CefString,要用宽字符设置,最后返回的json字符串也是CefString,也要用wstring保存,不然中文会乱码。

最新文章

  1. MySQL日期时间函数大全 转
  2. [转贴]有关Angular 2.0的一切
  3. 与众不同 windows phone (46) - 8.0 通信: Socket, 其它
  4. c++11 内存模型解读
  5. Python [Leetcode 342]Power of Four
  6. Computer Science Theory for the Information Age-1: 高维空间中的球体
  7. &quot;sessionFactory &quot; or &quot;hibernateTemplate &quot; is required异常
  8. IP Editor IP控件(对比一下封装IP控件)
  9. redis 5 种数据结构
  10. Python爬虫【解析库之pyquery】
  11. Eclipse远程调试+FTPClient在jdk6以上写法不兼容问题的排查
  12. 种类并查集(洛谷P2024食物链)
  13. FastJson、Jackson、Gson进行Java对象转换Json细节处理
  14. Codechef STMINCUT S-T Mincut (CodeChef May Challenge 2018) kruskal
  15. Unity Chan 3D Asset
  16. django 分页函数
  17. VNPY思维导图架构
  18. 常用软件安装及VS插件工具
  19. django的用户认证组件
  20. adb: failed to install xxx.apk: Failure [INSTALL_FAILED_UPDATE_INCOMPATIBLE: Package com.xxx.deliveryrobot signatures do not match the previously installed version; ignoring!]

热门文章

  1. Python_tkinter(5)_GUI工具
  2. 2.Hadoop平台架构准备工作
  3. tarjan算法(强连通分量 + 强连通分量缩点 + 桥(割边) + 割点 + LCA)
  4. A-the Beatles
  5. xpath详细讲解
  6. Docker 共有 13 个管理命令和 41 个通用命令,以下是常用 Docker 命令列表
  7. sql多行多列重复
  8. node概述
  9. 迷宫问题 (bfs广度优先搜索记录路径)
  10. Linux交换Esc和Caps