cocos2d-x JsonBox 读写

时间:2023-03-08 16:16:57
#include "JsonBox.h"
std::string path = "test.json”;
//注意引入路径
path = cocos2d::CCFileUtils::sharedFileUtils()->fullPathForFilename(path.c_str());
/*写入文件*/
JsonBox::Object o;
o["myName"] = JsonBox::Value();
o["myOtherMember"] = JsonBox::Value("asld\\kfn");
o["hahaha"] = JsonBox::Value(true);
o["adamo"] = JsonBox::Value(129.09);
std::cout << o << std::endl;
JsonBox::Value v(o);
v.writeToFile(path); /*读取文件*/
JsonBox::Value v2;
v2.loadFromFile(path);
std::cout << v2 << std::endl; /*读取json语句*/
JsonBox::Array a;
a.push_back(JsonBox::Value("I'm a string..."));
a.push_back(JsonBox::Value());
std::cout << a << std::endl;
void JsonTest::Create() {
JsonBox::Value v1;
char *json =
"{\"menu\":{\"id\":\"file\",\"value\":\"File\",\"popup\":{\"menuitem\":[{\"value\":\"New\",\"onclick\":\"CreateNewDoc()\"},{\"value\":\"Open\",\"onclick\":\"OpenDoc()\"},{\"value\":\"Close\",\"onclick\":\"CloseDoc()\"}]}}}";
v1.loadFromString(json);
std::string temp = v1["menu"]["id"].getString();
CCLOG("json.transform=%s", "start");
char* str = strdup(temp.c_str());
CCLOG("json.menu.id=%s", str); JsonBox::Array arr = v1["menu"]["popup"]["menuitem"].getArray();
CCLOG("arr.lenth=%d", arr.size());
for (int i = ; i < arr.size(); i++) {
std::string t = arr[i]["value"].getString();
str = strdup(t.c_str());
CCLOG("arr.item=%s", str);
}
}