您的位置:首页 > 移动开发 > Cocos引擎

Cocos2D_X中的文本操作(LibJson)

2013-10-29 15:49 381 查看
首先.h中声明一个方法: void ParseJSON(const JSONNode & n);

再cpp文件中添加以下这行代码:

JSONNode n(JSON_NODE);

n.push_back(JSONNode("RootA","Value in parent node"));

JSONNode c(JSON_ARRAY);

n.set_name("ChildNode");

JSONNode c1(JSON_NODE),c2(JSON_NODE);

c1.push_back(JSONNode("AAAA", "xxxxxxxxxxx"));

c2.push_back(JSONNode("BBBB", "xyc123456789"));

c2.push_back(JSONNode("CCCC", ".........."));

n.push_back(c);

n.push_back(c1);

n.push_back(c2);

cout<<n.write_formatted()<<endl;

ParseJSON(n);

return true;

调用void ParseJSON(const JSONNode & n)方法

void HelloWorld::ParseJSON(const JSONNode & n)

{

JSONNode::const_iterator i = n.begin();

while (i != n.end()) {

if (i ->type() ==JSON_ARRAY|| i->type() == JSON_NODE) {

ParseJSON(*i);

}

std::string node_name = i->name();

if (node_name=="RootA") {

json_string rootA = i->as_string();

cout<<rootA<<endl;

}

else if (node_name == "AAAA")

{

json_string AAAA = i->as_string();

cout<<AAAA<<endl;

}

else if (node_name == "BBBB")

{

json_string BBBB = i->as_string();

}

++i;

}

}

Xcode编译结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: