JsonCpp 库如何集成到Visual studio

这是用于 在 C++ 中解析和生成 JSON 数据 的工具包,适合在需要与前端、网络、配置等 JSON 格式交互的 C++ 项目中使用。

Json(基于JsonCpp)

格式全称:JavaScript Object Notation

格式特点:

  1. 与开发语言无关
  2. 轻量级的数据存储格式
  3. 易于解析和封装
  4. 数据对象内部一般采用key-value模式来存储
  5. 支持数组、字符串、整数、小数、对象、布尔、空
  6. 可以用于数据传输、数据保存、数据交换

格式详解:

{

"name":"张三",

"age":18,

"身高":1.81,

"生日":"2002-01-02",

"学校":"北大青鸟",

"专业":"厨师","挖掘机",

"单身":true,

"地址":null,

"好友":{...}

}

开源项目:

通用版本:(推荐)

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

Window专用版本:

https://github.com/Microsoft/vcpkg.git

vcpkg: https://github.com/Microsoft/vcpkg.githttps://gitee.com/yao_yu_qq/vcpkg

一、如何下载最简单方式:直接集成源代码(推荐给初学者)

步骤:

  1. 下载 JsonCpp 源码:

进入 GitHub 页面下载:

或者直接克隆:

复制代码
git clone https://github.com/open-source-parsers/jsoncpp.git

2.找到源代码文件夹:

  • 进入 jsoncpp/src/lib_json/,将以下文件复制到你的项目中:

  • json_reader.cpp

  • json_value.cpp

  • json_writer.cpp

  • json.h

  • json_forwards.h(新版本可能已经合并)

3.包含头文件 + 编译源码

在你的 C++ 项目中添加这些源文件,然后包含头文件:

复制代码
#include "json/json.h"

示例编译命令(假设使用 g++):

复制代码
g++ main.cpp json_reader.cpp json_writer.cpp json_value.cpp -o my_app

二、使用 vcpkg 自动安装(适合 Windows + VS 用户)

步骤:

1.安装 vcpkg(如果还没装):

复制代码
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
.\bootstrap-vcpkg.bat

2.安装 JsonCpp:

复制代码
.\vcpkg install jsoncpp

3.在你的项目中集成(以 Visual Studio 为例):

  • 在项目属性 → C/C++ → 常规 → 附加包含目录中添加:
    vcpkg\installed\x64-windows\include

  • 链接库目录也添加:
    vcpkg\installed\x64-windows\lib

4.在代码中使用:

复制代码
#include <json/json.h>

三、在 Visual Studio 项目中使用 JsonCpp

示例步骤:

1. 创建一个空的 Win32 控制台项目(或 CMake 项目也可以)
2. 添加以下测试代码:
复制代码
#include <iostream>
#include <json/json.h>

int main() {
    Json::Value person;
    person["name"] = "张三";
    person["age"] = 18;
    person["single"] = true;

    Json::StreamWriterBuilder writer;
    std::string json_output = Json::writeString(writer, person);

    std::cout << json_output << std::endl;
    return 0;
}
3. 直接编译运行(VS 会自动链接 JsonCpp)
相关推荐
郝学胜_神的一滴15 小时前
C++11 工程级应用 12:编译期类型魔法,干掉重复与臃肿的代码
c++·visual studio
水饺编程18 小时前
第5章,[Win32 章节] :在平面直角坐标系中描点
c语言·c++·windows·visual studio
学习智者19 小时前
《玄》IDE v3.6.3重磅发布:全功能修复与性能飞跃
开发语言·c++·ide·算法·中文语言 玄
重生之小比特20 小时前
【Java SE】IDEA 调试 Debug 案例完整分析
java·ide·intellij-idea
郝学胜_神的一滴20 小时前
C++11 工程级应用 11:编译期黑魔法,告别重复烂代码
c++·visual studio
Danny_姜1 天前
Android Studio Quail 4 稳定版发布:本地 Gemma 4 + Android Skills
android·ide·android studio
VIP_CQCRE1 天前
Visual Studio 也能接入 AI 编程:用 Ace Data Cloud 快速配置 LMLocal
openai·ai编程·visual studio·ace data cloud
withoutfear2 天前
IntelliJ IDEA卡顿内存分配不足和索引被频繁触发问题解决办法
java·ide·intellij-idea
an86950012 天前
unity如何在visual studio中打断点debug
unity·游戏引擎·visual studio
典典分享指南2 天前
DeepSeek Harness 开源贡献手记:从 Issue 到 Merge 的完整旅程
jupyter·github·vim·idea·visual studio