Cocos2d-x 3.0 加入了rapidjson库用于json解析。位于external/json下。

rapidjson 项目地址:http://code.google.com/p/rapidjson/wiki:http://code.google.com/p/rapidjson/wiki/UserGuide

下面就通过实例代码讲解rapidjson的用法。

使用rapidjson解析json串

  1. 引入头文件

    #include "json/rapidjson.h"
    #include "json/document.h"
  2. json解析

    std::string str = "{\"hello\" : \"word\"}";
    CCLOG("%s\n", str.c_str());
    rapidjson::Document d;
    d.Parse<0>(str.c_str());
    if (d.HasParseError()) //打印解析错误
    {
    CCLOG("GetParseError %s\n",d.GetParseError());
    } if (d.IsObject() && d.HasMember("hello")) { CCLOG("%s\n", d["hello"].GetString());//打印获取hello的值
    }
  3. 打印结果

    cocos2d: {"hello" : "word"}
    
    cocos2d: word

注意:只支持标准的json格式,一些非标准的json格式不支持。

一些常用的解析方法需要自己封装。注意判断解析节点是否存在。

使用rapidjson生成json串

  1. 引入头文件

    #include "json/document.h"
    #include "json/writer.h"
    #include "json/stringbuffer.h"
    using namespace rapidjson;
  2. 生成json串

    rapidjson::Document document;
    document.SetObject();
    rapidjson::Document::AllocatorType& allocator = document.GetAllocator();
    rapidjson::Value array(rapidjson::kArrayType);
    rapidjson::Value object(rapidjson::kObjectType);
    object.AddMember("int", 1, allocator);
    object.AddMember("double", 1.0, allocator);
    object.AddMember("bool", true, allocator);
    object.AddMember("hello", "你好", allocator);
    array.PushBack(object, allocator); document.AddMember("json", "json string", allocator);
    document.AddMember("array", array, allocator); StringBuffer buffer;
    rapidjson::Writer<StringBuffer> writer(buffer);
    document.Accept(writer); CCLOG("%s",buffer.GetString());
  3. 打印结果

     cocos2d: {"json":"json string","array":[{"int":1,"double":1,"bool":true,"hello":"你好"}]}

(27)Cocos2d-x 3.0 Json用法的更多相关文章

  1. Cocos2d-x 3.0 Json用法 Cocos2d-x xml解析

    Cocos2d-x 3.0 加入了rapidjson库用于json解析.位于external/json下. rapidjson 项目地址:http://code.google.com/p/rapidj ...

  2. C++通过jsoncpp类库读写JSON文件-json用法详解

    介绍: JSON 是常用的数据的一种格式,各个语言或多或少都会用的JSON格式. JSON是一个轻量级的数据定义格式,比起XML易学易用,而扩展功能不比XML差多少,用之进行数据交换是一个很好的选择. ...

  3. JSON: JSON 用法

    ylbtech-JSON: JSON 用法 1. JSON Object creation in JavaScript返回顶部 1. <!DOCTYPE html> <html> ...

  4. .NET3.5中JSON用法以及封装JsonUtils工具类

    .NET3.5中JSON用法以及封装JsonUtils工具类  我们讲到JSON的简单使用,现在我们来研究如何进行封装微软提供的JSON基类,达到更加方便.简单.强大且重用性高的效果. 首先创建一个类 ...

  5. 高屋建瓴 cocos2d-x-3.0架构设计 Cocos2d (v.3.0) rendering pipeline roadmap(原文)

    Cocos2d (v.3.0) rendering pipeline roadmap Why (the vision) The way currently Cocos2d does rendering ...

  6. linux find命令中-print0和xargs中-0的用法

    linux find命令中-print0和xargs中-0的用法. 1.默认情况下, find命令每输出一个文件名, 后面都会接着输出一个换行符 ('\n'), 因此find 的输出都是一行一行的: ...

  7. Eclipse配置tomcat8.5.7报错:The Apache Tomcat installation at this directory is version 8.5.27. A Tomcat 8.0 installation is...

    Eclipse配置tomcat8.5.7报错:The Apache Tomcat installation at this directory is version 8.5.27. A Tomcat ...

  8. json用法常见错误

    Json用法三个常见错误   net.sf.json.JSONException: java.lang.NoSuchMethodException

  9. 问题:c# newtonsoft.json使用;结果:Newtonsoft.Json 用法

    Newtonsoft.Json 用法 Newtonsoft.Json 是.NET 下开源的json格式序列号和反序列化的类库.官方网站: http://json.codeplex.com/ 使用方法 ...

随机推荐

  1. Linux上运行Jmeter

    上传jmeter到Linux服务器 unzip解压 配置环境变量vi /etc/profile: export PATH=/tmp/apache-jmeter-3.0/bin/:$PATH 刷新环境变 ...

  2. ftp服务通信操作

    1.将本地虚拟机网卡设置ip--->2.将虚拟机系统的网卡ip设置--->3.虚拟机设置特定网络模式vm8nat模式: (1) (2) (3) 保证正常互ping 通信, 4.在虚拟机系统 ...

  3. Lua脚本和C++交互(二)

    上一节讲了一些基本的Lua应用,下面,我要强调一下,Lua的栈的一些概念,因为这个确实很重要,你会经常用到.熟练使用Lua,最重要的就是要时刻知道什么时候栈里面的数据是什么顺序,都是什么.如果你能熟练 ...

  4. sql语句如何删除最后一条和第一条信息

    这是先前建好的SQL数据库中的test表, sql语句: delete a from test a,(select max(id) id from test) b where a.id = b.id ...

  5. android编译make错误——"javalib.jar invalid header field”、"classes-full-debug.jar 错误 41 "

    错误:读取 out/target/common/obj/JAVA_LIBRARIES/core-tests_intermediates/javalib.jar 时出错:invalid header f ...

  6. 使用OpenRowSet操作Excel Excel导入数据库

    使用 OpenRowSet 和 OpenDataSource 访问 Excel 97-2007 测试文件:D:\97-2003.xls和D:\2007.xlsx,两个文件的内容是一模一样的. 测试环境 ...

  7. min-height的兼容性问题

    1.经测试 IE+和其它主流浏览器均支持min-height属性,已经满足目前的需求. 2.当height和min-height同时设置时,浏览器自动选择数值更大的一个(测试IE7+及其他主流浏览器) ...

  8. Vue如何引入远程JS文件

    直接在dom上操作: export default { mounted() { const s = document.createElement('script'); s.type = 'text/j ...

  9. IIS的安装和配置

    一.首先是安装IIS.打开控制面板,找到“程序与功能” 二. “打开或关闭Windows功能”, 安装 “Internet 信息服务” 三. 安装完后回控制面板里面,找到“管理工具” 四. 双击“In ...

  10. 【BZOJ4429】[Nwerc2015] Elementary Math小学数学 最大流

    [BZOJ4429][Nwerc2015] Elementary Math小学数学 Description Ellen给她的学生教小学数学.期末考试已经来临了.考试有n个题目,每一个题目学生们都要对一 ...