C++解析Json的方法详解【jsoncpp】

本文实例讲述了C++解析Json的方法。分享给大家供大家参考,具体如下:

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,和xml类似,本文主要对VS2008中使用Jsoncpp解析json的方法做一下记录。

Jsoncpp是个跨平台的开源库,下载地址:http://sourceforge.net/projects/jsoncpp/,我下载的是v0.5.0,压缩包大约104K。

方法一:使用Jsoncpp生成的lib文件

解压上面下载的Jsoncpp文件,在jsoncpp-src-0.5.0/makefiles/vs71目录里找到jsoncpp.sln,用VS2008版本编译,默认生成静态链接库。 在工程中引用,只需要包含include/json下的头文件及生成的.lib文件即可。

如何包含lib文件:在.cpp文件中#pragma comment(lib.”json_vc71_libmt.lib”),在工程属性中Linker下Input中Additional Dependencies写入lib文件名字(Release下为json_vc71_libmt.lib,Debug为json_vc71_libmtd.lib)

注意:Jsoncpp的lib工程编译选项要和VS工程中的编译选项保持一致。如lib文件工程编译选项为MT(或MTd),VS工程中也要选择MT(或MTd),否则会出现编译错误问题,debug和release下生成的lib文件名字不同,注意不要看错了,当成一个文件来使用(我就犯了这个错误)。

方法二:使用Jsoncpp包中的.cpp和.h文件

解压上面下载的Jsoncpp文件,把jsoncpp-src-0.5.0文件拷贝到工程目录下,将jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\include\json和jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\src\lib_json目录里的文件包含到VS工程中,在VS工程的属性C/C++下General中Additional Include Directories包含头文件目录.\jsoncpp-src-0.5.0\include。在使用的cpp文件中包含json头文件即可,如:#include “json/json.h”。将json_reader.cpp、json_value.cpp和json_writer.cpp三个文件的Precompiled Header属性设置为Not Using Precompiled Headers,否则编译会出现错误。

jsoncpp 使用详解

jsoncpp 主要包含三种类型的 class:Value、Reader、Writer。jsoncpp 中所有对象、类名都在 namespace Json 中,包含 json.h 即可。Json::Value 只能处理 ANSI 类型的字符串,如果 C++ 程序是用 Unicode 编码的,最好加一个 Adapt 类来适配。

下面是从网上找的代码示例:

1. 从字符串解析json

const char* str = "{\"uploadid\": \"UP000000\",\"code\": 100,\"msg\": \"\",\"files\": \"\"}";Json::Reader reader;Json::Value root;if (reader.parse(str, root)) // reader将Json字符串解析到root,root将包含Json里所有子元素{  std::string upload_id = root["uploadid"].asString(); // 访问节点,upload_id = "UP000000"  int code = root["code"].asInt();  // 访问节点,code = 100}

2. 从文件解析json

int ReadJsonFromFile(const char* filename){  Json::Reader reader;// 解析json用Json::Reader  Json::Value root; // Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array  std::ifstream is;  is.open (filename, std::ios::binary );  if (reader.parse(is, root, FALSE))  {    std::string code;    if (!root["files"].isNull()) // 访问节点,Access an object value by name, create a null member if it does not exist.      code = root["uploadid"].asString();    code = root.get("uploadid", "null").asString();// 访问节点,Return the member named key if it exist, defaultValue otherwise.    int file_size = root["files"].size(); // 得到"files"的数组个数    for(int i = 0; i < file_size; ++i) // 遍历数组    {      Json::Value val_image = root["files"][i]["images"];      int image_size = val_image.size();      for(int j = 0; j < image_size; ++j)      {        std::string type = val_image[j]["type"].asString();        std::string url = val_image[j]["url"].asString();        printf("type : %s, url : %s \n", type.c_str(), url.c_str());      }    }  }  is.close();  return 0;}

3. 向文件中插入json

void WriteJsonData(const char* filename){  Json::Reader reader;  Json::Value root; // Json::Value是一种很重要的类型,可以代表任意类型。如int, string, object, array  std::ifstream is;  is.open (filename, std::ios::binary );  if (reader.parse(is, root))  {    Json::Value arrayObj;  // 构建对象    Json::Value new_item, new_item1;    new_item["date"] = "2011-11-11";    new_item1["time"] = "11:11:11";    arrayObj.append(new_item); // 插入数组成员    arrayObj.append(new_item1); // 插入数组成员    int file_size = root["files"].size();    for(int i = 0; i < file_size; ++i)      root["files"][i]["exifs"] = arrayObj;  // 插入原json中    std::string out = root.toStyledString();    // 输出无格式json字符串    Json::FastWriter writer;    std::string strWrite = writer.write(root);    std::ofstream ofs;    ofs.open("test_write.json");    ofs << strWrite;    ofs.close();  }  is.close();}

备注:Json试用不当会导致程序崩溃

Json::Value root;Json::Reader reader;

最好作为main函数的变量,不要作为全局变量,不要多次声明(即,不要在循环或者在其他函数中声明)。因为其static属性,在第一次使用结束后会被析构,后来的使用就会访问无效地址。

json_value.cpp中

Value::~Value() { switch (type_) { case nullValue: case intValue: case uintValue: case realValue: case booleanValue:  break; case stringValue:  if (allocated_)   releaseStringValue(value_.string_);  break; case arrayValue: case objectValue:  delete value_.map_;//!!!!!!  break; default:  JSON_ASSERT_UNREACHABLE; }

正确的使用方式如下:

int getRebalancing(string str, Json::Value root, Json::Reader reader) ;int main() {  Json::Value root;  Json::Reader reader;  while(1){    getRebalancing(string::str, root, reader);    //do something    }  return 0;}

PS:这里再为大家推荐几款比较实用的json在线工具供大家参考使用:

在线JSON代码检验、检验、美化、格式化工具:http://tools.jb51.net/code/json

JSON在线格式化工具:http://tools.jb51.net/code/jsonformat

在线XML/JSON互相转换工具:http://tools.jb51.net/code/xmljson

json代码在线格式化/美化/压缩/编辑/转换工具:http://tools.jb51.net/code/jsoncodeformat

C语言风格/HTML/CSS/json代码格式化美化工具:http://tools.jb51.net/code/ccode_html_css_json

希望本文所述对大家C++程序设计有所帮助。

做对的事情比把事情做对重要。

C++解析Json的方法详解【jsoncpp】

相关文章:

你感兴趣的文章:

标签云: