TestRAPIdJson.h文件:
#ifndef __TestRAPIdJson_SCENE_H__#define __TestRAPIdJson_SCENE_H__#include "cocos2d.h"#include "network\httpRequest.h"#include "network\httpClIEnt.h"#include "network\httpResponse.h"USING_NS_CC;using namespace cocos2d::network;class TestRAPIdJson : public cocos2d::Layer{public: // there's no 'ID' in cpp,so we recommend returning the class instance pointer static cocos2d::Scene* createScene(); // Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'ID' in cocos2d-iphone virtual bool init(); voID complete(httpClIEnt *clIEnt,httpResponse *response); // implement the "static create()" method manually CREATE_FUNC(TestRAPIdJson);};#endif // __TestRAPIdJson_SCENE_H__
TestRAPIdJson.cppc文件:
#include "TestRAPIdJson.h"//#include "cocos2d\external\Json\rAPIdJson.h"//#include "cocos2d\external\Json\document.h"//#include "cocos2d\external\Json\stringbuffer.h"//#include "cocos2d\external\Json\writer.h"#include "Json\rAPIdJson.h"#include "Json\document.h"#include "Json\stringbuffer.h"#include "Json\writer.h"Scene* TestRAPIdJson::createScene(){ // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auto layer = TestRAPIdJson::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene;}// on "init" you need to initialize your instancebool TestRAPIdJson::init(){ ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } //post auto postReq = new httpRequest(); postReq->setTag("type post"); postReq->setUrl("http://httpbin.org/post"); postReq->setRequestType(httpRequest::Type::POST); std::vector<std::string> header; header.push_back("Content-Type:application/Json;charset=utf-8"); postReq->setheaders(header); const char* reqData = "response Data"; postReq->setRequestData(reqData,strlen(reqData)); postReq->setResponseCallback(CC_CALLBACK_2(TestRAPIdJson::complete,this)); auto clIEnt2 = httpClIEnt::getInstance(); clIEnt2->send(postReq); postReq->release(); return true;}voID TestRAPIdJson::complete(httpClIEnt *clIEnt,httpResponse *response){ log("request tag is:%s",response->gethttpRequest()->getTag()); log("response code is:%d",response->getResponseCode()); if(response->isSucceed()) { /*std::vector<char> * data = response->getResponseData(); log("response data is:"); for (int i = 0; i < data->size(); i++) { log("%c",(*data)[i]); }*/ std::vector<char> * data = response->getResponseData(); std::stringstream oss; for (int i = 0; i < data->size(); i++) { oss<<(*data)[i]; } std::string str = oss.str(); log("response data is:%s",str.c_str());//=============解析Json(增删改查)================== rAPIdJson::document doc; doc.Parse<0>(str.c_str()); if (doc.HasParseError()) { log("Json parse error : %s",doc.GetParseError()); }else//解析成功之后的 *** 作 { log("parse success"); /* HasMember方法:判断是否有这个键值对 IsObject方法:判断是否是对象 Isstring方法:判断是否是字符串 IsArray方法:判断是否是数组 */ if (doc.IsObject()&&doc.HasMember("data")) { //取值 rAPIdJson::Value &value = doc["data"]; if (value.Isstring()) { log("data is :%s",value.GetString()); } } if (doc.IsObject()&&doc.HasMember("Json")) { //设值 doc["Json"].SetInt(15); log("Json is :% d",doc["Json"].GetInt() ); } //声明一个类型的数据(#include "Json\stringbuffer.h") rAPIdJson::StringBuffer buffer; //创建一个rAPIdJson::Writer对象 rAPIdJson::Writer<rAPIdJson::StringBuffer> writer(buffer); doc.Accept(writer); //这样就可以获取字符串了 log("Json string is: %s",buffer.GetString()); rAPIdJson::document::AllocatorType &allocator=doc.GetAllocator(); //添加整数 doc.AddMember("int",20,allocator); //添加字符串 doc.AddMember("string","string value",allocator); //添加一个null对象 rAPIdJson::Value nullObj(rAPIdJson::kNullType); doc.AddMember("null",nullObj,allocator); //添加了一个对象 rAPIdJson::Value obj(rAPIdJson::kObjectType); obj.AddMember("name","xiaoli",allocator); obj.AddMember("age",allocator); obj.AddMember("height",180,allocator); doc.AddMember("personInfo",obj,allocator); //添加一个数组 rAPIdJson::Value arr(rAPIdJson::kArrayType); arr.PushBack(1,allocator); arr.PushBack("string in array",allocator); rAPIdJson::Value obj1(rAPIdJson::kObjectType); obj1.AddMember("name",allocator); obj1.AddMember("age",allocator); obj1.AddMember("height",allocator); arr.PushBack(obj1,allocator); doc.AddMember("arr",arr,allocator); //添加完之后,重新打印输出本Json rAPIdJson::StringBuffer buffer1; rAPIdJson::Writer<rAPIdJson::StringBuffer> writer1(buffer1); doc.Accept(writer1); log("modifIEd data is: %s",buffer1.GetString()); } //=============解析Json================== }else { log("error msg is:%s",response->getErrorBuffer()); }}///*//request tag is:type post//response code is:200//response data is:{//"args": {},//"data": "response Data",//"files": {},//"form": {},//"headers": {//"Accept": "*/*",//"Accept-EnCoding": "deflate,gzip",//"Content-Length" : "13",//"Content-Type" : "application/Json;charset=utf-8",//"Host" : "httpbin.org"// },// "Json": null,// "origin" : "171.39.213.117",// "url" : "http://httpbin.org/post"//}////*/总结
以上是内存溢出为你收集整理的cocos2d-x解析Json(使用rapidjson)全部内容,希望文章能够帮你解决cocos2d-x解析Json(使用rapidjson)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)