Method of Generating Json file for Cocos2d-x (continued, haha), cocos2d-xjson
1. upgraded version of the Json Generation Tool
The upgraded version of the Json generation tool is encapsulated. You only need to input two vectors and one int value to generate the Json file.
2. Specific Code
1). h file
#include "cocos2d.h"#include "../cocos2d/external/json/document.h"#include "../cocos2d/external/json/writer.h"#include "../cocos2d/external/json/stringbuffer.h"USING_NS_CC;using namespace rapidjson;class JsonMake : public Ref{public: CREATE_FUNC(JsonMake); virtual bool init(); std::string getJsonMyBaodanMain(const std::vector<std::string>& strName,const std::vector<std::string>& strContent,const int count);};
2). cpp File
#include "JsonMake.h"bool JsonMake::init(){ bool bRet = false; do{ bRet = true; }while(0); return bRet;}std::string JsonMake::getJsonMyBaodanMain(const std::vector<std::string>& strName,const std::vector<std::string>& strContent,const int count){ rapidjson::Document document; document.SetObject(); CCASSERT(strName.size() == strContent.size(), "strName.size() != strContent.size()"); log("strName.size = %lu,strContent.size = %lu",strName.size(),strContent.size()); rapidjson::Document::AllocatorType& allocator = document.GetAllocator(); rapidjson::Value objectROOT(rapidjson::kArrayType); log("count = %d",count); for(int i = 0; i < count; i++) { rapidjson::Value object(rapidjson::kObjectType); for(int j = 0 ; j < strName.size(); j++) { object.AddMember(strName.at(j).c_str(), strContent.at(j).c_str(), allocator); } objectROOT.PushBack(object,allocator); } document.AddMember("info", objectROOT, allocator); StringBuffer buffer; rapidjson::Writer<StringBuffer> writer(buffer); document.Accept(writer); log("%s",buffer.GetString()); return StringUtils::format("%s",buffer.GetString());}
3. Usage
Add the following code to the init function in HelloWorldScene. cpp:
auto path = FileUtils::getInstance()->getWritablePath(); log("path = %s",path.c_str()); path.append("myBaodanMain.json"); FILE* fp = std::fopen(path.c_str(), "at+"); CCASSERT(fp != NULL, "file open error"); auto jsonMake = JsonMake::create(); std::vector<std::string> strName = getStrName(); std::vector<std::string> strContent = getStrContent(); auto str = jsonMake->getJsonMyBaodanMain(strName,strContent,10); auto length = str.length(); fwrite(str.c_str(), length, 1 ,fp); fclose(fp);
Then add the following two function definitions. Of course the Declaration in. h is essential!
Std: vector <std: string> HelloWorld: getStrName () {std: vector <std: string> strName; strName. push_back ("baodanTitle"); strName. push_back ("baodanStatue"); strName. push_back ("insuranceName"); strName. push_back ("takeEffectTime"); strName. push_back ("timeLimit"); strName. push_back ("saleUnit"); strName. push_back ("institutionAddress"); strName. push_back ("toubaoPerson"); strName. push_back ("beibaoPerson"); strName. push_back ("forehead"); strName. push_back ("premium"); return strName;} std: vector <std: string> HelloWorld: getStrContent () {std: vector <std: string> strContent; strContent. push_back ("policy no."); strContent. push_back ("policy status"); strContent. push_back ("Insurance name"); strContent. push_back ("Effective date"); strContent. push_back ("warranty period"); strContent. push_back ("Sales Unit"); strContent. push_back ("institution address"); strContent. push_back ("policyholder"); strContent. push_back ("insured"); strContent. push_back ("guaranteed"); strContent. push_back ("premium"); return strContent ;}
4. Results
How does php generate a json file in this format?
$ Arr1 = array ();
$ Arr3 = array ('code', 'name', 'add', 'colle4 ');
$ Arr4 = array ("","","","");
// Use a loop below to process the data similar to the following, so I will not write it.
$ Arr4 [0] = "001 ";
$ Arr4 [1] = "Name 1 ";
$ Arr4 [2] = "Address 11 ";
$ Arr4 [3] = col4 data ";
$ Arrtmp = array_combine ($ arr3, $ arr4 );
$ Arr5 = array ();
Array_push ($ arr5, $ arrtmp );
// The above is the end of the loop. $ arr5 contains the code, name, addr, and col4 data.
$ Arr2 = array ('Total' => "239", 'rows '=> $ arr5 );
Echo json_encode ($ arr2 );
How to generate a json file ?? Urgent
The eval () function in JQuery can be written in the following way on the JSP page:
Function ArrToJson (){
Var arr = new Array ();
// Arr The data you want to convert to json format
Var jsonData = eval (arr );
}