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

JS开发者转C++:如何用RapidJSON实现基于参考JSON的JSON值对比?

嘿,作为刚从JavaScript转去用C++和RapidJSON的开发者,我太懂这种转换时的懵圈感了!先结合你给出的JS代码片段,帮你把「基于参考JSON对比另外两个JSON」的功能用RapidJSON实现出来~

用RapidJSON实现JSON属性对比功能

先明确核心需求

从你提供的JS代码片段来看,testHasAllProperties应该是用来检查目标JSON是否包含参考列表里的所有必填属性。我们把这个功能扩展为:以第三个参考JSON为基准,验证另外两个JSON是否具备基准里的所有指定属性。

具体实现步骤

1. 准备工作

首先确保你已经引入了RapidJSON的核心头文件:

#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <iostream>
#include <vector>
#include <string>

2. 提取参考JSON的属性列表

先写一个辅助函数,从参考JSON里提取所有需要检查的属性名(对应你JS里的mandatoryProperties数组):

using namespace rapidjson;

// 从参考JSON对象中提取所有顶层属性名
std::vector<std::string> getMandatoryProperties(const Document& refDoc) {
    std::vector<std::string> props;
    if (!refDoc.IsObject()) {
        return props;
    }
    for (auto it = refDoc.MemberBegin(); it != refDoc.MemberEnd(); ++it) {
        props.push_back(it->name.GetString());
    }
    return props;
}

3. 实现属性检查函数

对应你JS里的testHasAllProperties,我们写一个函数来检查目标JSON是否包含所有必填属性:

// 检查目标JSON是否包含所有必填属性
bool hasAllProperties(const Document& targetDoc, const std::vector<std::string>& mandatoryProps) {
    if (!targetDoc.IsObject()) {
        return false;
    }
    for (const auto& prop : mandatoryProps) {
        if (!targetDoc.HasMember(prop.c_str())) {
            std::cout << "Missing required property: " << prop << std::endl;
            return false;
        }
        // 如果需要验证属性类型,可以在这里扩展,比如:
        // if (!targetDoc[prop.c_str()].IsString()) {
        //     std::cout << "Property " << prop << " is not a string!" << std::endl;
        //     return false;
        // }
    }
    return true;
}

4. 完整对比示例

下面是一个完整的示例,用来对比两个JSON是否都符合参考JSON的要求:

int main() {
    // 参考JSON(对应你JS里必填属性的来源)
    const char* refJson = R"({"input1": "", "input2": "", "result": ""})";
    // 待检查的JSON A
    const char* jsonA = R"({"input1": "test", "input2": "demo", "result": "ok"})";
    // 待检查的JSON B
    const char* jsonB = R"({"input1": "test", "result": "fail"})";

    // 解析参考JSON
    Document refDoc;
    refDoc.Parse(refJson);
    if (refDoc.HasParseError()) {
        std::cerr << "Failed to parse reference JSON!" << std::endl;
        return 1;
    }

    // 提取所有必填属性
    auto mandatoryProps = getMandatoryProperties(refDoc);

    // 解析并检查JSON A
    Document docA;
    docA.Parse(jsonA);
    if (docA.HasParseError()) {
        std::cerr << "Failed to parse JSON A!" << std::endl;
        return 1;
    }
    bool aPass = hasAllProperties(docA, mandatoryProps);
    std::cout << "JSON A check result: " << (aPass ? "✅ Pass" : "❌ Fail") << std::endl;

    // 解析并检查JSON B
    Document docB;
    docB.Parse(jsonB);
    if (docB.HasParseError()) {
        std::cerr << "Failed to parse JSON B!" << std::endl;
        return 1;
    }
    bool bPass = hasAllProperties(docB, mandatoryProps);
    std::cout << "JSON B check result: " << (bPass ? "✅ Pass" : "❌ Fail") << std::endl;

    return 0;
}

额外提示

  • 如果你的需求是深度对比嵌套属性,可以扩展上面的两个函数,递归遍历嵌套的JSON对象。
  • RapidJSON的Document是核心解析对象,注意它默认是只读的,如果需要修改JSON内容,可以使用Value类来操作。
  • 和JS的弱类型不同,C++是强类型语言,所以你可以很方便地在检查属性时验证类型(比如IsString()、IsInt()等),这在JS里通常不需要特意处理。

内容的提问来源于stack exchange,提问作者A.v

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:38:14