cocos2dx jsoncpp
jsoncpp下载 http://sourceforge.net/projects/jsoncpp/
下载解压后用到的是include\json下面的头文件跟src\lib_json下的文件。
导入头文件
#include "json/json.h"
解析json
void parseJsoncpp()
{
/*{
"name":"more-top",
"age":27
}*/
unsigned long size;
char *file = (char *)CCFileUtils::sharedFileUtils()->getFileData("jsoncpptest.json","r",&size);
Json::Reader reader;
Json::Value root;
if (!reader.parse(std::string(file),root,false))
{
return ;
}
CCLog("jsoncpptest"); std::string name = root["name"].asString();
CCLog("name = %s",name.c_str()); int age = root["age"].asInt();
CCLog("name = %d",age); //---------------------------------------
/*[
{"name":"more11","age":100},
{"name":"more22","age":101}
]*/ file = (char *)CCFileUtils::sharedFileUtils()->getFileData("jsoncpptest2.json","r",&size);
if (!reader.parse(std::string(file),root,false))
{
return ;
}
CCLog("jsoncpptest2"); size = root.size();
for (int i = ; i < size; i++)
{
std::string name = root[i]["name"].asString();
CCLog("name = %s",name.c_str()); int age = root[i]["age"].asInt();
CCLog("name = %d",age); }
}
输出:
jsoncpptest
name = more-top
name = 27
jsoncpptest2
name = more11
name = 100
name = more22
name = 101
创建json
void createJsoncpp()
{
//[{"age":18,"name":"cocos2dx"},{"age":20,"name":"Hello"}]
Json::FastWriter writer;
Json::Value root;
Json::Value person; person["name"] = "cocos2dx";
person["age"] = ;
root.append(person); person["name"] = "Hello";
person["age"] = ;
root.append(person);
string json_file = writer.write(root); CCLog("%s",json_file.c_str());
char filePath[] = {'\0'};
memset(filePath,,sizeof(filePath));
strcat(filePath,CCFileUtils::sharedFileUtils()->getWritablePath().c_str());
strcat(filePath,"writeJsoncpp.json"); FILE *file = fopen(filePath,"w+"); fwrite(json_file.c_str(),json_file.size(),,file);
fclose(file); }
输出:
[{"age":18,"name":"cocos2dx"},{"age":20,"name":"Hello"}]
cocos2dx jsoncpp的更多相关文章
- (原创)cocos2dx使用jsoncpp的正确姿势
环境: vs2010, cocos2dx@2.1.4, win32, jsoncpp-src-0.5.0.tar.gz jsoncpp下载地址: http://sourceforge.net/proj ...
- cocos2d-x 读取 json 文件并用 jsoncpp 做解析
一码胜万言(请看注释) CclUtil.h // // CclUtil.h // PracticeDemo // // Created by kodeyang on 8/1/13. // // #if ...
- Cocos2d-x 3.x游戏开发之旅
Cocos2d-x 3.x游戏开发之旅 钟迪龙 著 ISBN 978-7-121-24276-2 2014年10月出版 定价:79.00元 516页 16开 内容提要 <Cocos2d-x ...
- 【Cocos2d-x游戏开发】解决Cocos2d-x中文乱码的三种方法
众所周知,Cocos2d-x是一款不错的开源引擎,但是在Cocos2d-x中直接使用中文是无法正确显示的.比如下面的情况: 解决这个问题常用的有三种方法:1.通过转换为UTF-8编码来显示.2.使用i ...
- 《Cocos2d-x实战 C++卷》上线了-源码-样章-感谢大家的支持
<Cocos2d-x实战 C++卷>上线了 感谢大家一直以来的支持! 全面介绍Cocos开发技巧,采用Cocos2d-x3.2版本,并且详细介绍跨平台移植已经多平台发布细节. · 各大商店 ...
- 【转】jsoncpp在xcode中的使用
http://blog.csdn.net/ashqal/article/details/8573392 考虑到cocos2dx需要使用jsoncpp做关卡的设置, 尝试用源代码直接放到项目以方便后期生 ...
- 《Cocos2d-x实战(卷Ⅰ):C++开发》
<Cocos2d-x实战(卷Ⅰ):C++开发> 基础篇 第1章 准备开始 1.1 本书学习路线图 1.2 使用实例代码 第2章 Cocos2d-x介绍与环境搭建 ...
- cocos2d-x高级学习
弱联网开发技术: libcurl 添加lib文件:libcurl_imp.lib pthreadVCE2.lib 添加头文件:#include"curl/curl.h" curl ...
- jsoncpp解析拼装数组
Cocos2d-x添加jsoncpp应该资料都有了,今天来讲讲数组的解析和拼装- int main() { 数组创建与分析: 例子一: string strValue = "{\" ...
随机推荐
- 使用DDMS测试安卓手机APP的性能(android)
安装/配置: 通过另外一个工具也可以测试手机客户端APP的性能,这就是android开发包中的DDMS工具(Dalvik Debug Monitor Service),先来说一下android开发包的 ...
- 再谈Jquery Ajax方法传递到action(转)
之前写过一篇文章Jquery Ajax方法传值到action,本文是对该文的补充. 假设 controller中的方法是如下: public ActionResult ReadPerson(Perso ...
- Unity 2D两种常用判断点击的方法
1.Raycast法 原理相同于3D中得Raycast法,具体使用略有区别. RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorl ...
- Multiclass Classification
之前我们都是在Binary classification的基础上学习算法和知识. 如何使用Binary classification算法进行Multiclass classification呢? (一 ...
- POJ 2280&&hdu 1661
题意:给定平面上的N个点,属性分别标记为0和1,然后找一条直线,直线上的点全部溶解,一侧的1溶解,另一侧的0溶解.求出最多能溶解的点的个数. 思路:暴力枚举每个点,扫描线旋转.先做优化,如果一侧溶解0 ...
- 国外PHP学习网站书籍资料汇总
这篇文章汇总了全面多样的PHP学习资源,包括书籍.网站.文章等,帮助你提高PHP开发能力,巩固PHP知识.结合上一篇<重磅资料!Github上的PHP资源汇总大全>,一定可以让你的PHP技 ...
- DB2 递归查询
上一篇中讲解了ORACLE中的递归查询,下面我们看一下DB2中如何使用递归查询: 同样的我们先新建一个表来存储以上信息,并插入测试数据: --建表 create table FAMILY ( pers ...
- JSF 2 radio buttons example
In JSF, "h:selectOneRadio" tag is used to render a set of HTML input element of type " ...
- uva 10274 Fans and Gems(隐式图搜索+模拟)
Fans and Gems Input: Standard Input Output: Standard Output Tomy's fond of a game called 'Fans and G ...
- POJ 3237 Tree (树链剖分 路径剖分 线段树的lazy标记)
题目链接:http://poj.org/problem?id=3237 一棵有边权的树,有3种操作. 树链剖分+线段树lazy标记.lazy为0表示没更新区间或者区间更新了2的倍数次,1表示为更新,每 ...