json的解析

来源:互联网 发布:蘑菇街衣服比淘宝便宜 编辑:程序博客网 时间:2024/05/03 06:26
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://sourceforge.net/projects/jsoncpp/  
JsonCpp下载

win32中的使用方法:
1)在项目中,添加现有文件->导入include和src的文件
2)如果是引入别人的项目,就要自己配置头文件,并重新添加文件,否则可能会找不到文件

2.libJson——有多个语言版本,C++,JAVA,C#等(JSON只i是一种命名规则,因此可以通过多种语言处理)
http://sourceforge.net/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实例

  1. void HelloWorld::readJsonCpp()
  2. {
  3. unsigned long size;
  4. char* file = (char*)CCFileUtils::sharedFileUtils()->getFileData("testjson.json","r", &size);

  5. Json::Reader reader;
  6. Json::Value root;
  7. if (!reader.parse(std::string(file),root, false))
  8. {
  9.   return ;
  10. } std::string name = root["name"].asString();
  11. int age = root["age"].asInt(); CCLOG("name=%s",name.c_str());
  12. CCLOG("age=%d",age); ////////////////////////////////////
  13. file = (char*)CCFileUtils::sharedFileUtils()->getFileData("testjson2.json","r", &size);
  14. if (!reader.parse(std::string(file),root, false))
  15. {
  16.   return ;
  17. } size = root.size();
  18. for (int i=0; i<size; ++i)
  19. {
  20.   name = root[i]["name"].asString();
  21.   age = root[i]["age"].asInt();
  22.   CCLOG("name=%s,age=%d",name.c_str(),age);
  23. }
  24. }
  25. void HelloWorld::writeJsonCpp()
  26. {
  27. Json::Value root;
  28. Json::FastWriter writer;
  29. Json::Value person; person["name"] = "hello world";
  30. person["age"] = 100;
  31. root.append(person); std::string json_file = writer.write(root);
  32. char filePath[1024]= {'\0'};
  33. memset(filePath,0,sizeof(filePath));
  34. strcat(filePath,CCFileUtils::sharedFileUtils()->getWriteablePath().c_str());
  35. strcat(filePath,"writeJsonCpp.json"); FILE* file = fopen(filePath,"w+"); fwrite(json_file.c_str(),json_file.size(),1,file); fclose(file);
  36. }
复制代码

libJson实例
testlibjson.json内容如下

  1. {
  2. "RootA" : "Value in parent node",
  3. "ChildNode" : [
  4.   {
  5.    "ChildA" : "String Value c1",
  6.    "ChildB" : "dsf c1"
  7.   },
  8.   {
  9.    "ChildA" : "String Value c2",
  10.    "ChildB" : "dsf c2"
  11.   }
  12. ]
  13. }
复制代码
  1. void HelloWorld::readLibJson()
  2. {
  3.         //read json
  4.         unsigned long size;
  5.         char* str = (char*)CCFileUtils::sharedFileUtils()->getFileData("testlibjson.json","r", &size);

  6.         if(libjson::is_valid(str)==false)  {  
  7.                 delete str;  
  8.                 str=NULL;  
  9.                 printf("Parse faild!\n");  
  10.                 system("pause");  
  11.                 exit(0);
  12.         } 
  13.         JSONNode rn=libjson::parse(str);
  14.         delete str; 
  15.         str=NULL; 
  16.         int tmp = rn.size();
  17.         CCLOG("%d",tmp);
  18.         for (int i=0;i<rn[1].size();i++)
  19.         {
  20.                 JSONNode temp=rn[1][i];
  21.                 for(int j=0;j<temp.size();j++)
  22.                 {
  23.                         CCLOG("%s:%s",temp[j].name().c_str(),temp[j].as_string().c_str());
  24.                 }
  25.         }
  26. }

  27. void HelloWorld::writeLibJson()
  28. {
  29.         //write json
  30.         JSONNode n(JSON_NODE);
  31.         n.push_back(JSONNode("RootA", "Value in parent node"));
  32.         JSONNode c(JSON_ARRAY);
  33.         c.set_name("ChildNode");

  34.         JSONNode c1(JSON_NODE),c2(JSON_NODE);
  35.         c1.push_back(JSONNode("ChildA","String Value c1"));
  36.         c1.push_back(JSONNode("ChildB","dsf c1"));
  37.         c2.push_back(JSONNode("ChildA","String Value c2"));
  38.         c2.push_back(JSONNode("ChildB","dsf c2"));

  39.         c.push_back(c1);
  40.         c.push_back(c2);
  41.         n.push_back(c);
  42.         CCLOG("==%s",n.write_formatted().c_str());     //write_farmatted可以获取n的内容

  43.         unsigned long size;
  44.         char filePath[1024]= {'\0'};
  45.         memset(filePath,0,sizeof(filePath));
  46.         strcat(filePath,CCFileUtils::sharedFileUtils()->getWriteablePath().c_str());
  47.         strcat(filePath,"testlibjson.json");


  48.         FILE* file = fopen(filePath,"w+");

  49.         fwrite(n.write_formatted().c_str(),n.write_formatted().size(),1,file);   //write_farmtted可以获取文档内容的大小

  50.         fclose(file);
  51. }
复制代码
0 0