JSON格式 [{key:value,key2:value2},{key3:value3}] 1.[]里面的是数组 2.{}里面的是集合 3.用:隔开key和value 4.逗号分割每个数据单元,数据单元分为数组和元数据(即一个key或者value) 例子 简单的集合 {"name":"norton","age":100} 数组里面可以有集合 [{"name":"Dark","age":20},{"name":"Norton","age",40}] 集合里面可以包含数组 [{"name":"Dark","age":20},{"name":"Norton","age",40, [{"name":"norton","age":100},{"name":"norton","age":100}] }] value可以为数组 {"array",[{"name":"Dark","age":20},{"name":"Norton","age",40}]} JSON第三方库: 1.JsonCpp http:///projects/jsoncpp/ JsonCpp下载 win32中的使用方法: 1)在项目中,添加现有文件->导入include和src的文件 2)如果是引入别人的项目,就要自己配置头文件,并重新添加文件,否则可能会找不到文件 2.libJson——有多个语言版本,C++,JAVA,C#等(JSON只i是一种命名规则,因此可以通过多种语言处理) http:///projects/libjson/ libJson download 这个库也是很常用的。配置如下: 1)解压文件后,添加JSONOptions.h , libjson.h 2)然后把D:\cocos2d-x\tools\libjson_7.6.1\libjson\_internal\Source下的所有文件添加到项目中。以上两步可以新建一个文件夹libJson进行。 3)注意,libJson有两个使用库,C/C++,编译时候要指定。且win32下,默认是release而不是Debug模式的。所以Debug模式下会报错。 4) 如3所述,要在加载的头文件中,找到头文件JSONOptions.h 搜索#define JSON_DEBUG,保持打开(即不要注释该行).但如果你要发布,进行release编译,就要注释该行 搜索#define JSON_LIBRARY,如果要用C++编译,则注释该行。因为cocos2d-x下使用的是C++,所以要注释该行(默认是不注释的) 这样就配置完毕了 JsonCpp实例 void HelloWorld::readJsonCpp() { unsigned long size; char* file = (char*)CCFileUtils::sharedFileUtils()->getFileData("testjson.json","r", &size); Json::Reader reader; Json::Value root; if (!reader.parse(std::string(file),root, false)) { return ; } std::string name = root["name"].asString(); int age = root["age"].asInt(); CCLOG("name=%s",name.c_str()); CCLOG("age=%d",age); //////////////////////////////////// file = (char*)CCFileUtils::sharedFileUtils()->getFileData("testjson2.json","r", &size); if (!reader.parse(std::string(file),root, false)) { return ; } size = root.size(); for (int i=0; i<size; ++i) { name = root[i]["name"].asString(); age = root[i]["age"].asInt(); CCLOG("name=%s,age=%d",name.c_str(),age); } } void HelloWorld::writeJsonCpp() { Json::Value root; Json::FastWriter writer; Json::Value person; person["name"] = "hello world"; person["age"] = 100; root.append(person); std::string json_file = writer.write(root); char filePath[1024]= {'\0'}; memset(filePath,0,sizeof(filePath)); strcat(filePath,CCFileUtils::sharedFileUtils()->getWriteablePath().c_str()); strcat(filePath,"writeJsonCpp.json"); FILE* file = fopen(filePath,"w+"); fwrite(json_file.c_str(),json_file.size(),1,file); fclose(file); } libJson实例 testlibjson.json内容如下 { "RootA" : "Value in parent node", "ChildNode" : [ { "ChildA" : "String Value c1", "ChildB" : "dsf c1" }, { "ChildA" : "String Value c2", "ChildB" : "dsf c2" } ] } void HelloWorld::readLibJson() { //read json unsigned long size; char* str = (char*)CCFileUtils::sharedFileUtils()->getFileData("testlibjson.json","r", &size); if(libjson::is_valid(str)==false) { delete str; str=NULL; printf("Parse faild!\n"); system("pause"); exit(0); } JSONNode rn=libjson::parse(str); delete str; str=NULL; 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()); } } } void HelloWorld::writeLibJson() { //write json 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()); //write_farmatted可以获取n的内容 unsigned long size; char filePath[1024]= {'\0'}; memset(filePath,0,sizeof(filePath)); strcat(filePath,CCFileUtils::sharedFileUtils()->getWriteablePath().c_str()); strcat(filePath,"testlibjson.json"); FILE* file = fopen(filePath,"w+"); fwrite(n.write_formatted().c_str(),n.write_formatted().size(),1,file); //write_farmtted可以获取文档内容的大小 fclose(file); } 原文链接: http://bbs./thread-220577-1-1.html
|
|
来自: 拳毛弧的藏经阁 > 《Cocos2d-X》