#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文件
#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 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());
}