在Cocos2dx3.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\n",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()); }
原文地址:https://www.cnblogs.com/ldxsuanfa/p/10847900.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。