【C++】使用JsonCpp

Json语法是 JavaScript 对象表示语法的子集。所以在Java,JavaScript等语言中使用起来是十分愉快的。在C++中我们使用跨平台的开源库JsonCpp也能愉快的玩耍Json。

下载地址

GitHub:https://github.com/open-source-parsers/jsoncpp

添加到工程

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

  • 解压上面下载的jsoncpp-master.zip文件,把jsoncpp-master\include\json文件夹和jsoncpp-master\src\lib_json文件夹里的全部文件拷贝到工程目录下,并且添加到到VS工程中。
  • 在需要使用JsonCpp的文件中包含json头文件即可,如:#include "json/json.h"。另外,需要将json_reader.cpp、json_value.cpp和json_writer.cpp三个文件的Precompiled Header属性设置为Not Using Precompiled Headers,否则编译会出现错误。

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

  • 解压上面下载的jsoncpp-master.zip文件,在jsoncpp-master\makefiles\vs71目录里找到jsoncpp.sln,用VS编译,默认生成静态链接库。 在工程中引用,只需要包含include/json下的头文件及生成的.lib文件即可。
  • 在需要使用JsonCpp的文件中添加#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),否则会出现编译错误问题。

JsonCpp 使用详解

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

  • 通过字符串创建Json对象
std::string strValue = “{\”key\”:\”value1\”,\
\”array\”:[{\"arraykey\":1},{\"arraykey\":2}]}”; 

Json::Reader reader; 
Json::Value root; 
// reader将Json字符串解析到root,root将包含Json里所有子元素
if (reader.parse(strValue, root))   
{ 
   if (!root["key"].isNull())
   {
    std::string strValue= root["key"].asString(); 
    std::cout << strValue<< std::endl; 
  }
  Json::Value arrayObj = root["array"]; 
  for (int i=0; i<arrayObj.size(); i++) 
  { 
    int iarrayValue = arrayObj[i]["arraykey"].asInt(); 
    std::cout << iarrayValue << std::endl;  
  } 
}
  • 构建Json对象序列化为字符串
Json::Value root; 
Json::Value arrayObj;
Json::Value item; 

root["key"] = “value1″; 
for (int i=0; i<10; i++)
{ 
  item["arraykey"] = i; 
  arrayObj.append(item);  //添加新的数组成员
} 
root["array"] = arrayObj; 
std::string out = root.toStyledString();  //将Json对象序列化为字符串
std::cout << out << std::endl;
  • 向文件中插入Json对象
void WriteJsonData(const char* filename)
{    
  Json::Reader reader;
  Json::Value root;
  ifstream is;
  is.open(filename, std::ios::binary);
  if (reader.parse(is, root, FALSE))
  {
    Json::Value item; 

    root["key"] = “value1″; 
    //添加数组成员
    item["arraykey"] = 2; 
    root["array"].append(item)

    Json::FastWriter writer;
    string strWrite = writer.write(root);
    ofstream ofs;
    ofs.open(filename);
    ofs << strWrite;
    ofs.close();
  }
  is.close();  
}
  • 清空Json对象中的数组
root["array"].resize(0);
  • 删除Json对象
root.removeMember("key");


作者:caesar1228
链接:https://www.jianshu.com/p/987e95cc79f4
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C++使用JsonCpp库来读取JSON文件,你可以按照以下步骤进行操作: 1. 首先,确保已经下载并安装了JsonCpp库,并将其包含在你的项目中。 2. 创建一个Json::Value对象来存储解析后的JSON数据。 3. 使用Json::Reader对象来读取JSON文件并解析为Json::Value对象。 下面是一个简单的示例代码,演示了如何使用JsonCpp读取JSON文件: ```cpp #include <iostream> #include <fstream> #include <json/json.h> int main() { // 读取 JSON 文件 std::ifstream jsonFile("example.json"); if (!jsonFile.is_open()) { std::cout << "Failed to open JSON file." << std::endl; return 1; } // 解析 JSON 数据 Json::Value root; Json::Reader reader; if (!reader.parse(jsonFile, root)) { std::cout << "Failed to parse JSON data." << std::endl; return 1; } // 读取 JSON 数据并输出 std::string name = root["name"].asString(); int age = root["age"].asInt(); std::string city = root["city"].asString(); std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; std::cout << "City: " << city << std::endl; return 0; } ``` 在上面的示例中,我们首先打开了一个名为`example.json`的JSON文件,然后使用JsonCppJson::Reader对象解析JSON数据,并将其存储在名为`root`的Json::Value对象中。接下来,我们通过访问`root`对象的键值对来获取JSON数据,并将其输出到控制台上。 请确保将示例代码中的`example.json`替换为你实际使用JSON文件的路径。 这里提供一个CSDN上的相关教程供参考:[https://blog.csdn.net/qq_41453285/article/details/105808014](https://blog.csdn.net/qq_41453285/article/details/105808014)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值