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. php学习十三:其他关键字

    在php中,其实不止在php中,在其他语言中我们也会常常接触到一些关键字,整理了一下php当中的一下关键字,可能有些不全,希望大家指出来,多多交流,一起进步. 1.final 特性:1.使用final ...

  2. Receiver type for instance message is a forward

    本文转载至 http://my.oschina.net/sunqichao/blog?disp=2&catalog=0&sort=time&p=3 这往往是引用的问题.ARC要 ...

  3. KVO的用法、底层实现原理

    KVO的用法 KVO也就是key-value-observing(即键值观察),利用一个key来找到某个属性并监听其值得改变.用法如下: 添加观察者 在观察者中实现监听方法,observeValueF ...

  4. 互斥锁mutex

    https://blog.csdn.net/rqc112233/article/details/50015069 //g++ mute.cpp -o mute -g -lrt -lpthread #i ...

  5. poj_3987 Trie图

    题目大意 有N个病毒,病毒由A-Z字母构成,N个病毒各不相同.给出一段程序P,由A-Z字母构成,若病毒在在程序P或者P的逆转字符串P'中存在,则该程序P被该病毒感染.求出程序P被多少种病毒感染. 题目 ...

  6. Go基础---->go的基础学习(二)

    这里记录的是go中函数的一些基础知识.道听途说终是浅,身临其境方知深. go的基础知识 一.go中函数的基础使用 package main import ( "fmt" " ...

  7. font-size对展示的影响

    实例: <head>        <style type="text/css">            span{display: inline-bloc ...

  8. Android长截屏-- ScrollView,ListView及RecyclerView截屏

    http://blog.csdn.net/wbwjx/article/details/46674157       Android长截屏-- ScrollView,ListView及RecyclerV ...

  9. MS17-010永恒之蓝验证

    一.安装MSF,windows下安装也可以,直接安装kali也可以,我是kali是攻击主机,win7是靶机,都在虚拟机里. 1.windows下安装MSF请参考:http://blog.csdn.ne ...

  10. 【BZOJ3011】[Usaco2012 Dec]Running Away From the Barn 可并堆

    [BZOJ3011][Usaco2012 Dec]Running Away From the Barn Description It's milking time at Farmer John's f ...