• CEF 支持JSON操作


    转载: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", 9527);
        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保存,不然中文会乱码。

  • 相关阅读:
    利用CSS3 中steps()制用动画
    移动WEB测试工具 Adobe Edge Inspect
    Grunt配置文件编写技巧及示范
    CSS3 box-shadow快速教程
    编写爬虫程序的神器
    node+express+jade搭建一个简单的"网站"
    node+express+ejs搭建一个简单的"页面"
    node的模块管理
    node的调试
    mongoDB的权限管理
  • 原文地址:https://www.cnblogs.com/chechen/p/10291562.html
Copyright © 2020-2023  润新知