zoukankan      html  css  js  c++  java
  • cocos2dx libjson

    libjson下载 http://sourceforge.net/projects/libjson/

    下载解压后改名成libjson,用到的是根目录下面的JSONOptions.h、libjson.h和_internalSource下的文件。

    导入头文件

    #include "libjson.h"

    如果是debug模式下,改下面那个参数

    把#define JSON_DEBUG注释打开,c++编码把#define JSON_LIBRARY注释

    如果是release模式则注释#define JSON_DEBUG

    解析json

    void readlibjson()
    {
        /*{
            "RootA":"value in parent node",
                "childNode":[
            {
                "childA":"string value c1",
                    "childB":"dsf c1"
            },
            {
                "childA":"string value c2",
                    "childB":"dsf c2"
                }
                ]
        }*/
        unsigned long size;
        char* str = (char *)CCFileUtils::sharedFileUtils()->getFileData("testlibjson.json","r",&size);
        if (libjson::is_valid(str) == false)
        {
            delete str;
            str = NULL;
            CCLog("parse fail!");
            return ;
        }
        JSONNode rn = libjson::parse(str);
        delete str;
        str = NULL;
        CCLog("%s:%s",rn[0].name().c_str(),rn[0].as_string().c_str());
        int tmp = rn.size();
        CCLog("%d",tmp);
        for (int i = 0; i<rn[1].size(); i++)
        {
            JSONNode temp = rn[1][i];
            for (int j = 0; j< temp.size(); j++)
            {
                CCLog("%s:%s",temp[j].name().c_str(),temp[j].as_string().c_str());
            }
    
        }
    
        parseJSON(rn);//递归解析json的结构
        
    }
    
    //递归解析json的结构
    void parseJSON(const JSONNode & n){
        JSONNode::const_iterator i = n.begin();
        while (i != n.end()){
            // recursively call ourselves to dig deeper into the tree
            if (i -> type() == JSON_ARRAY || i -> type() == JSON_NODE){
                parseJSON(*i);
            }
            if(i->name()!="" && i->as_string()!="")
            CCLog("%s:%s",i->name().c_str(),i->as_string().c_str());
            //increment the iterator
            ++i;
        }
    }

    输出:

    RootA:value in parent node
    2
    childA:string value c1
    childB:dsf c1
    childA:string value c2
    childB:dsf c2
    RootA:value in parent node
    childA:string value c1
    childB:dsf c1
    childA:string value c2
    childB:dsf c2

    创建json

    void writelibjson()
    {
        JSONNode n(JSON_NODE);
        n.push_back(JSONNode("RootA","value in parent node"));
        JSONNode c(JSON_ARRAY);
        c.set_name("childNode");
    
        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);
    
        CCLog("==%s",n.write_formatted().c_str());
    
        //下面是输出到保存文件
        unsigned long size;
        char filePath[1024] = {'/0'};
        memset(filePath,0,sizeof(filePath));
        strcat(filePath,CCFileUtils::sharedFileUtils()->getWritablePath().c_str());
        strcat(filePath,"testlibjson.json");
    
        FILE* file = fopen(filePath,"w+");
        fwrite(n.write_formatted().c_str(),n.write_formatted().size(),1,file);
        fclose(file);
    
    }

    输出:

    =={
    "RootA" : "value in parent node",
    "childNode" : [
    {
    "childA" : "string value c1",
    "childB" : "dsf c1"
    },
    {
    "childA" : "string value c2",
    "childB" : "dsf c2"
    }
    ]
    }

  • 相关阅读:
    美化博客园
    ansible的安装
    面向对象和类
    函数知识分类
    生成器
    内置函数_old
    迭代器
    装饰器
    Hadoop——MapReduce
    Hadoop——HDFS
  • 原文地址:https://www.cnblogs.com/ycclmy/p/4275020.html
Copyright © 2011-2022 走看看