Json 采用Jsoncpp , 从官网上下载源代码, 目录下面有2个目录:
1. jsoncpp/src/lib_json/ ,这里是json实现方法,要将以下文件加入到工程中
../../Classes/jsoncpp/src/lib_json/json_reader.cpp \
../../Classes/jsoncpp/src/lib_json/json_value.cpp \
../../Classes/jsoncpp/src/lib_json/json_writer.cpp \
2. jsoncpp/include/json ,这里需要设置为include路径, 具体跨平台设置头文件路径,请参考:
(http://zgame.blog.51cto.com/6144241/1066817)
$(LOCAL_PATH)/../../Classes/jsoncpp/include/json \
//-------------------------------------------------------------------
Json使用方法:
1. 解析
- #include "json.h"
- using namespace std;
- Json::Reader reader;
- Json::Value value;
- void GameBoard::readJson(std::string net_response) {
- //开始解析
- if(reader.parse(net_response, value))
- {
- //读取string
- Read_json_GameDataByString(value,"v");
- Read_json_GameDataByString(value,"performance");
- //解析数组
- Json::Value arrayObj = value["result"];
- for(unsigned int i = 0;i <arrayObj.size(); i++)
- {
- int errore = Read_json_GameDataByInt(arrayObj[i] ,"ecode");
- ....
- }
- }
- }
2. 发送
- void GameBoard::writeJson(std::string cmd, Json::Value params) {
- std::string ss;
- using namespace std;
- Json::FastWriter writer;
- Json::Value root;
- Json::Value arrayObj;
- Json::Value item;
- Json::Value dataArray;
- Json::Value dataItem;
- if (cmd == kNetGameInit)
- {
- _globalData.userInfo_.guid = "test";
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- if(!this->gameBoardDelegate->if_QQLogin()){
- char * ttt= this->gameBoardDelegate->Get_Andorid_Udid();
- _globalData.userInfo_.guid = * CCString::createWithFormat("%s",ttt);
- }
- else{
- // qq already set
- }
- CCLOG("_globalData.userInfo_.guid:%s",_globalData.userInfo_.guid.getCString());
- #endif
- item["guid"] = 0;
- item["code"] = 00;
- params["sync"]=0;
- }
- else
- {
- item["guid"] = _globalData.userInfo_.udid.m_sString;
- item["code"] = _globalData.userInfo_.authcode;
- params["sync"]=1;
- }
- item["uid"] = _globalData.userInfo_.guid.m_sString;
- item["pid"] = "2";
- item["t"] = "";
- item["v"] = "1.3.4";
- item["key"] = "1234";
- item["server"]="1";
- dataItem["cmd"] = cmd;
- dataItem["params"] = params;
- dataArray.append(dataItem); //数组的处理方式
- item["data"] = dataArray;
- ss = item.toStyledString();
- CCLOG("%s",ss.c_str());
- ss = writer.write(item);
- //json数据,http发送
- this->gameBoardDelegate->HttpDataSend(ss);
- }
数据包加密在http网络收数据后和发数据前处理就可以。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。