首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【C++】开源:jsoncpp库安装与使用入门

【C++】开源:jsoncpp库安装与使用入门

作者头像
DevFrank
发布2024-07-24 15:27:09
发布2024-07-24 15:27:09
2.5K0
举报

😏1. jsoncpp介绍

JsonCpp是一个开源的C++库,用于解析、生成和操作JSON格式数据。它支持标准的JSON语法,并具有良好的扩展性和可定制性。

该库提供了简单易用的API,可以轻松地实现JSON数据的读取、写入、修改和查询等操作。它还提供了丰富的错误处理机制和文档化的代码示例,使得初学者也能快速上手。

JsonCpp支持所有主流的C++编译器和操作系统平台,并且在多个开源项目中被广泛应用,如OpenCV、ROS等。同时,该库还提供了Python和Java等其他编程语言的绑定,方便跨语言使用。

JsonCpp是一个功能强大、易用性高、性能优秀的C++ JSON库,为JSON数据的处理提供了便利和效率。

😊2. jsoncpp安装

ubuntu apt安装比较简单:

代码语言:javascript
复制
sudo apt-get install libjsoncpp-dev

引用头文件:

代码语言:javascript
复制
#include "jsoncpp/json/json.h

编译:

代码语言:javascript
复制
g++ main.cpp -o main -ljsoncpp

😆3. jsoncpp入门使用

从字符串读取
代码语言:javascript
复制
#include "jsoncpp/json/json.h"
#include <iostream>
#include <memory>
/**
 * \brief Parse a raw string into Value object using the CharReaderBuilder
 * class, or the legacy Reader class.
 * Example Usage:
 * $g++ readFromString.cpp -ljsoncpp -std=c++11 -o readFromString
 * $./readFromString
 * colin
 * 20
 */
int main() {
  const std::string rawJson = R"({"Age": 20, "Name": "colin"})";
  const auto rawJsonLength = static_cast<int>(rawJson.length());
  constexpr bool shouldUseOldWay = false;
  JSONCPP_STRING err;
  Json::Value root;

  if (shouldUseOldWay) {
    Json::Reader reader;
    reader.parse(rawJson, root);
  } else {
    Json::CharReaderBuilder builder;
    const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
    if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root,
                       &err)) {
      std::cout << "error" << std::endl;
      return EXIT_FAILURE;
    }
  }
  const std::string name = root["Name"].asString();
  const int age = root["Age"].asInt();

  std::cout << name << std::endl;
  std::cout << age << std::endl;
  return EXIT_SUCCESS;
}
写入到字符串
代码语言:javascript
复制
#include "jsoncpp/json/json.h"
#include <iostream>
/** \brief Write a Value object to a string.
 * Example Usage:
 * $g++ stringWrite.cpp -ljsoncpp -std=c++11 -o stringWrite
 * $./stringWrite
 * {
 *     "action" : "run",
 *     "data" :
 *     {
 *         "number" : 1
 *     }
 * }
 */
int main() {
  Json::Value root;
  Json::Value data;
  constexpr bool shouldUseOldWay = false;
  root["action"] = "run";
  data["number"] = 1;
  root["data"] = data;	// 嵌套

  if (shouldUseOldWay) {
    Json::FastWriter writer;
    const std::string json_file = writer.write(root);
    std::cout << json_file << std::endl;
  } else {
    Json::StreamWriterBuilder builder;
    const std::string json_file = Json::writeString(builder, root);
    std::cout << json_file << std::endl;
  }
  return EXIT_SUCCESS;
}
从文件中读取

package.json

代码语言:javascript
复制
{
  "name": "tmp",
  "version": "1.0.0",
  "dependencies": {
  }
}

readFile.cpp

代码语言:javascript
复制
#include <iostream>
#include <fstream>
#include <jsoncpp/json/json.h>

using namespace std;

int main(){
   ifstream ifs("package.json");
   Json::Reader reader;
   Json::Value obj;
   reader.parse(ifs, obj);
   cout << " name " << obj["name"].asString() << endl;
   return 0;
}
写入到文件

fileWrite.cpp

代码语言:javascript
复制
#include <iostream>
#include <fstream>
#include <jsoncpp/json/json.h>

using namespace std;

int main(){
  fstream fs;
  fs.open("package_new.json",ios::out); // ios::out|ios::app为追加
  Json::Value root;
  Json::Value data;
  root["action"] = "run";
  data["number"] = 1;
  root["data"] = data;	// 嵌套

  Json::StreamWriterBuilder builder;
  const std::string json_file = Json::writeString(builder, root);
  // std::cout << json_file << std::endl;
  
  fs << json_file << endl;  // 写入文件
  fs.close();
  return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-07-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 😏1. jsoncpp介绍
  • 😊2. jsoncpp安装
  • 😆3. jsoncpp入门使用
    • 从字符串读取
    • 写入到字符串
    • 从文件中读取
    • 写入到文件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档