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. 引入头文件

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    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. 打印结果

    1
    2
    3
    cocos2d: {"hello" : "word"}
     
    cocos2d: word

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

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

使用rapidjson生成json串

  1. 引入头文件

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    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. 打印结果

    1
    cocos2d: {"json":"json string","array":[{"int":1,"double":1,"bool":true

    Cocos2d-x 已经加入了tinyxml2用于xml的解析。3.0版本位于external/tinyxml2下。2.x版本位于cocos2dx/support/tinyxml2下。

    tinyxml2 Github地址:https://github.com/leethomason/tinyxml2

    帮助文档地址:http://grinninglizard.com/tinyxml2docs/index.html

    生成xml文档

    1. 引入头文件

      1
      2
      #include "tinyxml2/tinyxml2.h"
      using namespace tinyxml2;
    2. xml文档生成

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      void  HelloWorld::makeXML(const char *fileName)
      {
      std::string filePath = FileUtils::getInstance()->getWritablePath() + fileName;
       
      XMLDocument *pDoc = new XMLDocument();
       
      //xml 声明(参数可选)
      XMLDeclaration *pDel = pDoc->NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
       
      pDoc->LinkEndChild(pDel);
       
      //添加plist节点
      XMLElement *plistElement = pDoc->NewElement("plist");
      plistElement->SetAttribute("version", "1.0");
      pDoc->LinkEndChild(plistElement);
       
      XMLComment *commentElement = pDoc->NewComment("this is xml comment");
      plistElement->LinkEndChild(commentElement);
       
      //添加dic节点
      XMLElement *dicElement = pDoc->NewElement("dic");
      plistElement->LinkEndChild(dicElement);
       
      //添加key节点
      XMLElement *keyElement = pDoc->NewElement("key");
      keyElement->LinkEndChild(pDoc->NewText("Text"));
      dicElement->LinkEndChild(keyElement);
       
      XMLElement *arrayElement = pDoc->NewElement("array");
      dicElement->LinkEndChild(arrayElement);
       
      for (int i = 0; i<3; i++) {
          XMLElement *elm = pDoc->NewElement("name");
          elm->LinkEndChild(pDoc->NewText("Cocos2d-x"));
          arrayElement->LinkEndChild(elm);
      }
       
      pDoc->SaveFile(filePath.c_str());
      pDoc->Print();
       
      delete pDoc;
      }
    3. 打印结果

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      <?xml version="1.0" encoding="UTF-8"?>
      <plist version="1.0">
      <!--this is xml comment-->
      <dic>
          <key>Text</key>
          <array>
              <name>Cocos2d-x</name>
              <name>Cocos2d-x</name>
              <name>Cocos2d-x</name>
          </array>
      </dic>
      </plist>

    上面代码使用tinyxml简单生成了一个xml文档。

    解析xml

    下面我们就来解析上面创建的xml文档

    1. 引入头文件

      1
      2
      #include "tinyxml2/tinyxml2.h"
      using namespace tinyxml2;
    2. xml解析

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      void HelloWorld::parseXML(const char *fileName)
      {
       
      std::string filePath = FileUtils::getInstance()->getWritablePath() + fileName;
      XMLDocument *pDoc = new XMLDocument();
      XMLError errorId = pDoc->LoadFile(filePath.c_str());
       
      if (errorId != 0) {
          //xml格式错误
          return;
      }
       
      XMLElement *rootEle = pDoc->RootElement();
       
      //获取第一个节点属性
      const XMLAttribute *attribute = rootEle->FirstAttribute();
      //打印节点属性名和值
      log("attribute_name = %s,attribute_value = %s", attribute->Name(), attribute->Value());
       
      XMLElement *dicEle = rootEle->FirstChildElement("dic");
      XMLElement *keyEle = dicEle->FirstChildElement("key");
      if (keyEle) {
          log("keyEle Text= %s", keyEle->GetText());
      }
       
      XMLElement *arrayEle = keyEle->NextSiblingElement();
      XMLElement *childEle = arrayEle->FirstChildElement();
      while ( childEle ) {
          log("childEle Text= %s", childEle->GetText());
          childEle = childEle->NextSiblingElement();
      }
       
      delete pDoc;
       
      }

      在节点解析过程中,注意对获取到的节点进行判空处理。

    3. 解析结果打印

      1
      2
      3
      4
      5
      cocos2d: attribute_name = version,attribute_value = 1.0
      cocos2d: keyEle Text= Text
      cocos2d: childEle Text= Cocos2d-x
      cocos2d: childEle Text= Cocos2d-x
      cocos2d: childEle Text= Cocos2d-x

    小结

    上面的简单示例,演示了如何使用tinyxml进行xml文档生成和解析。更多详细的帮助请参考 tinyxml帮助文档http://grinninglizard.com/tinyxml2docs/index.html

Cocos2d-x 3.0 Json用法 Cocos2d-x xml解析的更多相关文章

  1. (27)Cocos2d-x 3.0 Json用法

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

  2. UI进阶 解析XML 解析JSON

    1.数据解析 解析的基本概念 所谓“解析”:从事先规定好的格式中提取数据 解析的前提:提前约定好格式,数据提供方按照格式提供数据.数据获取方则按照格式获取数据 iOS开发常见的解析:XML解析.JSO ...

  3. 高屋建瓴 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 ...

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

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

  5. JSON: JSON 用法

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

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

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

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

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

  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. [NOIP2014] 解方程&加强版 (bzoj3751 & vijos1915)

    大概有$O(m)$,$O(n\sqrt{nm})$,$O(n\sqrt{m})$的3个算法,其中后2个可以过加强版.代码是算法3,注意bzoj的数据卡掉了小于20000的质数. #include< ...

  2. 用css进行布局

     用css进行布局 一,开始布局的注意事项 1.作为最佳实践,应把html(内容)和css(显示)分离: 2.网站设计主要有两大类型:固定宽度(基于像素)和响应式(也称流式,使用百分数定义) 二,构建 ...

  3. tomcat十大安全优化措施

    1.telnet管理端口保护 使用telnet连接进来可以输入SHUTDOWN可以直接关闭tomcat,极不安全,必须关闭.可以修改默认的管理端口8005改为其他端口,修改SHUTDOWN指令为其他字 ...

  4. aircrack-ng test

    Aircrack-ng工具包有很多工具,我用到的工具主要有以下几个: airmon-ng 处理网卡工作模式 airodump-ng 抓包 aircrack-ng 破解 aireplay-ng 发包,干 ...

  5. 关于Linux的内存(free -m)

    查看Linux服务器下的内存使用情况,可以使用命令free -m.注意此命令只在Linux下有效,在FreeBSD中没有此命令.命令如下所示: used:已经使用的内存数 free:空闲的内存数 sh ...

  6. 中缀表达式转后缀表达式(用于求字符串表达式值)(js栈和队列的实现是通过数组的push和unshift方法插值,pop方法取值)

    中缀表达式:就是我通常用的算术或逻辑公式: 后缀表达式:不包含括号,运算符放在两个运算对象后面,所有的计算按运算符出现的顺序,严格从左向右进行,不用考虑运算符优先级: 如,(2+1)*3 转换后,2 ...

  7. 9月19日下午JavaScript数组冒泡排列和二分法

    数组 一.冒泡排列 对数组attr = [1,8,6,4,5,3,7,2,9]进行由大到小排列,用冒泡排列的方法排列时,会对数组进行比较互换.如果前一个数字较大,这2个元素排列方式不变,如果后一个元素 ...

  8. Linux学习笔记(二)2015.4.14

    笔记2.1 Q:登陆命令  A:刚开始登陆的是安装Linux时设置的普通用户,如lin,输入su - root后,成为root用户 笔记2.2 Q:切换控制台  A:ctrl+alt+F1-F7可以切 ...

  9. centos6.5分区简易操作

    fdisk /dev/sdb --->n--->p---->输入分区大小(回车就默认全部大小) mkfs.ext4 /dev/sdb1 mkdir /data 在根目录下新建data ...

  10. ubuntu qq

    系统:Ubuntu 14.04  64位 1.下载qq国际版(直接网络搜索就可以) 2.解压并安装: # cp wine-qqintl.zip /usr/local/ # pwd/usr/local/ ...