zoukankan      html  css  js  c++  java
  • Cocos2d-x3.0 Json解析

    2dx3.0下一个JSON解析库官员以及集成。我们参考一下吧OK。

    JSON文件hello.json内容

    {"pets":["dog","cat"],"stuInfo":{"stuAge":"23","stuName":"zhangsan","birthday":"1990-01-12"},"username":"tomsfff","other":[true,30]}



    .h头文件

    #include "cocos-ext.h"
    #include "json/document.h"


    .cpp

     //获取文件路径
            const char* file_path = FileUtils::getInstance()->fullPathForFilename("hello.json").c_str();
           log("external file path = %s",file_path);
    
            
            rapidjson::Document d1;
            std::string contentStr = FileUtils::getInstance()->getStringFromFile(file_path);
            d1.Parse<0>(contentStr.c_str());
            //打印JSon文件的内容
            printf("%s
    ",contentStr.c_str());
            
            //获取JSon中数组的方法
            const rapidjson::Value& v = d1["pets"];
            if (v.IsArray()) {
                //这里一定要注意  变量i 一定要是   unsigned int  不然会报错
                for (unsigned int i = 0; i< v.Size(); ++i) {
                    
                     const rapidjson::Value &val = v[i];
                    log("%s",val.GetString());
                }
                
            }else
            {
                const rapidjson::Value& val = v["stuAge"];
                log("val.GetString() = %s",val.GetString());
                
            }
    


    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    Python作业之分页显示内容
    Codeforces Round #368 (Div. 2)
    数论专项测试——约数个数和(lucas的数论)
    数论专题测试——逆元
    数论专题测试——幸运数字
    bzoj2219: 数论之神
    bzoj3283: 运算器
    梅森素数
    后缀数组
    Hash_1014: [JSOI2008]火星人prefix
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4654122.html
Copyright © 2011-2022 走看看