cocos2dx 读取rapidjson及解析

本文介绍如何在Cocos2dx中使用RapidJSON读取并解析ball.json文件,具体步骤包括读取文件内容、解析JSON文档、验证成员存在性及获取所需数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cocos2dx 读取json及解析

  12170人阅读  评论(1)  收藏  举报
  分类:

ball.json 数据如下:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. {  
  2.     "entities": [  
  3.         {  
  4.             "entity": {  
  5.                 "TapOpposite": 0,   
  6.                 "Interval": 0.95,   
  7.                 "BallNum": 1  
  8.             }  
  9.         },   
  10.         {  
  11.             "entity": {  
  12.                 "TapOpposite": 0,   
  13.                 "Interval": 0.91,   
  14.                 "BallNum": 2  
  15.             }  
  16.         },   
  17.         {  
  18.             "entity": {  
  19.                 "TapOpposite": 0,   
  20.                 "Interval": 0.95,   
  21.                 "BallNum": 3  
  22.             }  
  23.         }  
  24.     ]  
  25. }  

在cocos2dx中json的读取是用的rapidjson,包含在cocostudio工程中。所以我们要先引入#include "cocostudio/CocoStudio.h"

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. void GameWorld::readJson()  
  2. {  
  3.     //json 文档  
  4.     rapidjson::Document _doc;  
  5.     bool bRet = false;  
  6.     ssize_t size = 0;  
  7.     unsigned char *pBytes = NULL;  
  8.     do {  
  9.         pBytes = cocos2d::CCFileUtils::sharedFileUtils()->getFileData("ball.json""r", &size);  
  10.         CC_BREAK_IF(pBytes == NULL || strcmp((char*)pBytes, "") == 0);  
  11.         std::string load_str((const char*)pBytes, size);  
  12.         CC_SAFE_DELETE_ARRAY(pBytes);  
  13.         _doc.Parse<0>(load_str.c_str());  
  14.         CC_BREAK_IF(_doc.HasParseError());            
  15.         //生成json文档对像  
  16.   
  17.         if(!_doc.IsObject())  
  18.             return;  
  19.   
  20.         //是否有此成员  
  21.         if(!_doc.HasMember("entities"))  
  22.             return;  
  23.   
  24.         // 通过[]取成员值,再根据需要转为array,int,double,string  
  25.         const rapidjson::Value &pArray = _doc["entities"];  
  26.   
  27.         //是否是数组  
  28.         if(!pArray.IsArray())  
  29.             return;  
  30.   
  31.         for (rapidjson::SizeType i = 0; i < pArray.Size(); i++)  
  32.         {  
  33.             const rapidjson::Value &p = pArray[i];                
  34.             if(p.HasMember("entity"))  
  35.             {  
  36.                 const rapidjson::Value &valueEnt = p["entity"];  
  37.                 if(valueEnt.HasMember("TapOpposite") && valueEnt.HasMember("Interval") && valueEnt.HasMember("BallNum"))  
  38.                 {  
  39.                     const rapidjson::Value &tapOpposite = valueEnt["TapOpposite"];  
  40.                     int tapOp = tapOpposite.GetInt();      //得到int值  
  41.   
  42.                     const rapidjson::Value &interval = valueEnt["Interval"];  
  43.                     float inter = interval.GetDouble();  //得到float,double值  
  44.   
  45.                     const rapidjson::Value &ballNum = valueEnt["BallNum"];  
  46.                     int ball = ballNum.GetInt();      //得到int值  
  47.   
  48.                     ballParam param;  
  49.                     param.tapOp = tapOp;  
  50.                     param.inter = inter;  
  51.                     param.ballIndex = ball;  
  52.                     m_ballParamVec.push_back(param);  
  53.                 }  
  54.             }  
  55.             else  
  56.             {  
  57.                 return;  
  58.             }  
  59.   
  60.         }  
  61.         bRet = true;  
  62.   
  63.     } while (0);  
  64. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值