您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。