【问题标题】:Add set(distinct elements) in rapidjson在 rapidjson 中添加集合(不同的元素)
【发布时间】:2020-09-01 04:05:30
【问题描述】:

我想创建以下 Json 对象

 {
    "name":"abc",
    "property1"=[2010, 2013, 2015],
    "property2"=["str1, "str2", str3"],
    "property3"=[true, false]
 }

property1, property2, property3 基本上是一个数组,但具有不同的元素。 目前我正在通过以下方式创建:

document.AddMember("property1", Value(kArrayType), allocator);
document.AddMember("property2", Value(kArrayType), allocator);
document.AddMember("property3", Value(kArrayType), allocator);

然后我遍历列表并将对象中的值通过:

for (auto& iter: object_info_list) {
    document["property1"].PushBack(iter->property1, allocator); //int value
    document["property2"].PushBack(iter->property2, allocator); // string value
    document["property3"].PushBack(iter->property3, allocator); // bool value
}

但是上面的语句也推送了所有的值(重复)。有没有办法只添加不同的值(比如在 c++ 中设置)

提前谢谢你

【问题讨论】:

  • 为什么不将list_of_working_years 设为std::set
  • @Wander3r 更新了问题。

标签: c++ json rapidjson


【解决方案1】:

JSON 没有很多数据类型。您需要在从中复制的 C++ 数据结构中删除重复数据。

例如

std::set<int> property1;
std::set<std::string> property2;
std::set<bool> property3;

for (auto& iter: object_info_list) {
    property1.insert(iter->property1);
    property2.insert(iter->property2);
    property3.insert(iter->property3);
}

for (auto val : property1) {
    document["property1"].PushBack(val, allocator);
}

for (auto val : property2) {
    document["property2"].PushBack(val, allocator);
}

for (auto val : property3) {
    document["property3"].PushBack(val, allocator);
}

【讨论】:

    猜你喜欢
    • 2022-12-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2020-10-19
    • 2015-03-26
    • 1970-01-01
    • 2016-02-23
    相关资源
    最近更新 更多