1、创建一个json格式的文件
bool AA::init()
{
//this->A();
JSONNode n(JSON_NODE);
n.push_back(JSONNode("Root","value in parent node"));
JSONNode c(JSON_ARRAY);
c.set_name("ChildName");
JSONNode c1(JSON_NODE),c2(JSON_NODE);
c1.push_back(JSONNode("childA","String value c1"));
c1.push_back(JSONNode("childB","dsf c1"));
c2.push_back(JSONNode("childA","String value c2"));
c2.push_back(JSONNode("childB","dsf c2"));
c.push_back(c1);
c.push_back(c2);
n.push_back(c);
std::cout<<n.write_formatted()<<std::endl;
this->PaserJson(n);
//给它传char *类型的也不会崩溃
//JSONNode n=libjson::parse(<#const json_string &json#>)
//this->fileBundle();
return true;
}
2、json格式的解析
首先应当导入一个libjson(库)
//json解析
static int j;
void AA:: PaserJson(const JSONNode & n)
{
JSONNode::const_iterator i=n.begin();
CCLog("zhengti");
while ( i != n.end() )
{
CCLog("fenbu");
if( i->type() == JSON_ARRAY || i->type() == JSON_NODE )
{
PaserJson(*i);
}
std::string node_name=i->name();
if(node_name=="Root")
{
json_string rootA=i->as_string();
std::cout<<rootA<<std::endl;
}
else if(node_name=="childA")
{
json_string childA=i->as_string();
std::cout<<childA<<std::endl;
}
else if(node_name=="childB")
{
json_string childB=i->as_string();
const char *b=childB.c_str();
std::cout<<childB<<std::endl;
}
++i;
}
j++;
CCLog("%d",j);
}