You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Visual C++项目中配置Jsoncpp?REST服务序列化需求指引

嘿,我来一步步帮你搞定在Visual Studio的C++项目里配置Jsoncpp,顺便给你演示怎么把类对象序列化成JSON——刚好能满足你调用REST服务的需求!

第一步:获取Jsoncpp库

你有两种常用方式获取这个库,选哪种看你的习惯:

  • 用vcpkg包管理器(推荐):
    1. 先安装vcpkg(如果还没装的话),打开PowerShell或者命令行,克隆vcpkg仓库后执行bootstrap脚本完成安装。
    2. 安装Jsoncpp:执行命令 vcpkg install jsoncpp:x64-windows(如果你的项目是x86平台,就把x64换成x86)。
    3. 把vcpkg集成到Visual Studio:执行 vcpkg integrate install,这样后续新建的项目会自动识别已安装的库,不用手动配置路径。
  • 手动编译源码:
    1. 去Jsoncpp的官方代码仓库下载最新的源码包。
    2. 用CMake生成Visual Studio工程:在源码根目录新建build文件夹,打开CMake-GUI,指定源码路径和build路径,点击Configure选择对应的VS版本,然后Generate生成工程文件。
    3. 打开生成的VS工程,分别编译Debug和Release版本,得到对应的静态库(jsoncppd.lib/jsoncpp.lib)或者动态库(jsoncppd.dll/jsoncpp.dll)。
第二步:在Visual Studio项目中配置Jsoncpp

如果用vcpkg集成过了,这一步可以跳过;如果是手动编译的库,按下面步骤来:
右键你的项目 → 属性(注意要选对当前的配置:Debug/Release,平台:x86/x64):

  • C/C++ → 常规:在「附加包含目录」里添加Jsoncpp源码中的include文件夹路径。
  • 链接器 → 常规:在「附加库目录」里添加你编译好的库文件所在的文件夹路径。
  • 链接器 → 输入:在「附加依赖项」里添加对应的库文件名:Debug模式加jsoncppd.lib,Release模式加jsoncpp.lib。
  • 如果用的是动态库,还要把编译出来的jsoncppd.dll(Debug)或jsoncpp.dll(Release)复制到你的项目输出目录(比如Debug/或Release/文件夹),否则运行时会找不到库。
第三步:类对象序列化/反序列化示例

接下来给你写个实际的例子,把自定义类转成JSON字符串(方便发REST请求),以及把JSON字符串转回类对象(解析REST响应):

#include <json/json.h>
#include <string>
#include <sstream>
#include <stdexcept>

// 定义你的业务类,比如一个用户类
class User {
public:
    int id;
    std::string name;
    int age;

    // 序列化:把User对象转换成Json::Value
    Json::Value toJson() const {
        Json::Value root;
        root["id"] = id;
        root["name"] = name;
        root["age"] = age;
        return root;
    }

    // 反序列化:把Json::Value转换成User对象
    static User fromJson(const Json::Value& root) {
        User user;
        // 注意:实际项目里最好加个判断,确保key存在且类型正确
        user.id = root["id"].asInt();
        user.name = root["name"].asString();
        user.age = root["age"].asInt();
        return user;
    }
};

// 辅助函数:把Json::Value转换成无格式的字符串(适合网络传输)
std::string jsonToString(const Json::Value& root) {
    Json::StreamWriterBuilder builder;
    builder["indentation"] = ""; // 去掉缩进,生成紧凑的JSON字符串
    return Json::writeString(builder, root);
}

// 辅助函数:把JSON字符串解析成Json::Value
Json::Value stringToJson(const std::string& jsonStr) {
    Json::CharReaderBuilder builder;
    Json::Value root;
    std::string parseErrors;
    std::istringstream jsonStream(jsonStr);

    if (!Json::parseFromStream(builder, jsonStream, &root, &parseErrors)) {
        throw std::runtime_error("JSON解析失败: " + parseErrors);
    }
    return root;
}

// 使用示例
int main() {
    // 1. 序列化对象为JSON字符串,用于发送REST请求
    User targetUser{101, "Balasubramanian", 32};
    Json::Value userJson = targetUser.toJson();
    std::string requestBody = jsonToString(userJson);
    // 这里就可以把requestBody作为POST请求的body发给REST服务了

    // 2. 反序列化示例:解析REST服务返回的JSON字符串
    std::string responseStr = R"({"id":101,"name":"Balasubramanian","age":32})";
    Json::Value responseJson = stringToJson(responseStr);
    User receivedUser = User::fromJson(responseJson);

    return 0;
}
一些要注意的小坑
  • 一定要保证配置的库版本和项目的Debug/Release、x86/x64完全匹配,不然会出现链接错误或者运行时崩溃。
  • 如果用vcpkg安装库,要注意安装命令里的平台参数和项目平台一致(比如x64-windows对应项目的x64平台)。
  • 实际项目中,序列化/反序列化时最好加一些错误判断(比如检查JSON里的key是否存在、类型是否正确),避免因为非法JSON导致程序崩溃。

内容的提问来源于stack exchange,提问作者Balasubramanian Ramamoorthi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:01:07