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

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

导入头文件

#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[].name().c_str(),rn[].as_string().c_str());
int tmp = rn.size();
CCLog("%d",tmp);
for (int i = ; i<rn[].size(); i++)
{
JSONNode temp = rn[][i];
for (int j = ; 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[] = {'/0'};
memset(filePath,,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(),,file);
fclose(file); }

输出:

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

cocos2dx libjson的更多相关文章

  1. cocos2d-x把json数据解析到数组或字典中(libjson库)

    以前在cocos2d-x项目中用到json解析,集成了libjson库后发现网上提供的解析方法大多是在解析过程中取得值,并没有将解析结果有效的保存起来,于是摸索一番,把解析结果根据数据格式存到数组或字 ...

  2. (原创)cocos2dx使用jsoncpp的正确姿势

    环境: vs2010, cocos2dx@2.1.4, win32, jsoncpp-src-0.5.0.tar.gz jsoncpp下载地址: http://sourceforge.net/proj ...

  3. cocos2dx json数据解析

    转自:http://blog.csdn.net/wangbin_jxust/article/details/9707873 cocos2dx本身没有json解析类库,我们这里引入libjson进行解析 ...

  4. cocos2d-x-Json/XML文件

    数据存储几种方式 1. 数据库 2. 文件 3. 内存 这里介绍Json格式与XML格式的文件存储 常用的文件存储数据的格式 1. Json格式 2. XML格式 Json适合存储小数据,XML适合存 ...

  5. cocos2d-x高级学习

    弱联网开发技术: libcurl 添加lib文件:libcurl_imp.lib  pthreadVCE2.lib 添加头文件:#include"curl/curl.h" curl ...

  6. cocos2d-x游戏引擎核心之十——网络通信

    一.建立基本的http通信并得到返回信息 1.创建cocos2dx工程 2.项目引用外部库 如果要使用cocos2dx的CCHttpClient来进行网络访问,则需要引入cocos2dx的相关库,详细 ...

  7. (转载)使用curl 和 libjson 完成联网和数据解析

    转载地址:http://my.oschina.net/cocosgame/blog/71181 libjson 编译和使用 - 3. libjson的C接口 API http://blog.csdn. ...

  8. cocos2dx调用浏览器打开网址

    安卓端cocos2dx/platform/android路径下CCApplication.h: virtual void openURL(const char* pszUrl); CCApplicat ...

  9. 使用“Cocos引擎”创建的cpp工程如何在VS中调试Cocos2d-x源码

    前段时间Cocos2d-x更新了一个Cocos引擎,这是一个集合源码,IDE,Studio这一家老小的整合包,我们可以使用这个Cocos引擎来创建我们的项目. 在Cocos2d-x被整合到Cocos引 ...

随机推荐

  1. 【LeetCode 208】Implement Trie (Prefix Tree)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  2. Probabilistic SVM 与 Kernel Logistic Regression(KLR)

    本篇讲的是SVM与logistic regression的关系. (一) SVM算法概论 首先我们从头梳理一下SVM(一般情况下,SVM指的是soft-margin SVM)这个算法. 这个算法要实现 ...

  3. Designing Evolvable Web API with ASP.NET 随便读,随便记 “The Internet,the World Wide Web,and HTTP”

    1982年,诞生了 Internet; 1989年,诞生了World Wide Web . "World Wide Web"的构造为主要由 三部分构成: resources 资源 ...

  4. c#调用c++ dll的几种类型(转)

    http://www.sosuo8.com/article-2012/dllleixingzhuanhuan.htm   在合作开发时,C#时常需要调用C++DLL,当传递参数时时常遇到问题,尤其是传 ...

  5. 【转】linux shell 正则表达式(BREs,EREs,PREs)差异比较

    我想各位也和我一样,再linux下使用grep,egrep, awk , sed, vi的搜索时,会经常搞不太清楚,哪此特殊字符得使用转义字符'\' ..   哪些不需要, grep与egrep的差异 ...

  6. hive-mysql安装配置

    默认情况下,hive的元数据信息存储在内置的Derby数据中.Facebook将hive元数据存储在关系数据库 1.安装好mysql ,sudo apt-get install mysql-serve ...

  7. 使用Async同步执行异步函数

    为了适应异步编程,减少回调的嵌套,我在项目中引入了Async,当批量处理且需要同步执行一些逻辑相同的异步函数时,觉得还是Async最为靠谱. 我有一个类似下面代码的场景,依据数组中的每一个元素执行一个 ...

  8. DedeCMS Error:Tag disabled:"php"的解决办法

  9. 第四次作业——WORDSEARCH小游戏

    “谁想出来的这么缺德的题目啊!!!!”一个声音在我心中回荡 这个题目很早就在课堂上公布了,我和我的小伙伴都惊呆了! 这是个毛?根本无从下手的感觉 总是觉得这个小游戏不是程序能给出答案的,因为我的第一印 ...

  10. 使用CocoaPods管理依赖库

    本篇内容将介绍Mac和iOS开发中必备的一个依赖库管理工具CocoaPods. CocoaPods是什么 在iOS开发中势必会用到一些第三方依赖库,比如大家都熟悉的ASIHttpRequest.AFN ...