Cocos2D_X中的文本操作(LibJson)

来源:互联网 发布:solarman软件下载 编辑:程序博客网 时间:2024/05/01 19:25

首先.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编译结果