Cocos2d-x3.0 Json解析
1501 ワード
2dx3.0下JSON解析ライブラリは公式に統合されているので、引用すればOKです.
JSONファイルjsonコンテンツ
.hヘッダファイル
.cpp
JSONファイルjsonコンテンツ
{"pets":["dog","cat"],"stuInfo":{"stuAge":"23","stuName":"zhangsan","birthday":"1990-01-12"},"username":"tomsfff","other":[true,30]}
.hヘッダファイル
#include "cocos-ext.h"
#include "json/document.h"
.cpp
//
const char* file_path = FileUtils::getInstance()->fullPathForFilename("hello.json").c_str();
log("external file path = %s",file_path);
rapidjson::Document d1;
std::string contentStr = FileUtils::getInstance()->getStringFromFile(file_path);
d1.Parse<0>(contentStr.c_str());
// JSon
printf("%s
",contentStr.c_str());
// JSon
const rapidjson::Value& v = d1["pets"];
if (v.IsArray()) {
// i unsigned int
for (unsigned int i = 0; i< v.Size(); ++i) {
const rapidjson::Value &val = v[i];
log("%s",val.GetString());
}
}else
{
const rapidjson::Value& val = v["stuAge"];
log("val.GetString() = %s",val.GetString());
}