Rapid json, c++, json, 修改空json数组

Rapid json, c++, json, modify empty json array

我有一个 json 文件,其中包含两个对象。第一个对象是一个对象数组。这些对象中的每一个都有一个元素“键”和空数组。我需要用 4 个数字填充数组,我需要保存回 json。我正在检查教程,但可能我遗漏了一些东西。有人可以帮我吗? 这是我的代码,它不起作用:

void BimObjectsToProjection::modifyViewBoxForProjection(std::string projectionName, long long minX, long long minY, long long Xlen, long long Ylen)
{
    Value& projections = md_FilesJsonDocument["ProjectionImages"];
    for (Value::ValueIterator projectionsIterator = projections.Begin(); projectionsIterator != projections.End(); ++projectionsIterator)
    {
        rapidjson::Value& projectionJson = *projectionsIterator;
        string name = projectionJson["Name"].GetString();
        if (projectionName == name)
        {
            Document::AllocatorType& allocator = md_FilesJsonDocument.GetAllocator();
            rapidjson::Value& viewBox = (*projectionsIterator)["BB"];

            viewBox.PushBack((int)minX, allocator);
            viewBox.PushBack((int)minY, allocator);
            viewBox.PushBack((int)Xlen, allocator);
            viewBox.PushBack((int)Ylen, allocator);
           
            rapidjson::StringBuffer strbuf;
            rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
            md_FilesJsonDocument.Accept(writer);
            break;
        }
    }
}

json 看起来像这样:

{
    "ProjectionImages": [
        {
            "Id": "33c75d31-7ccd-4daf-814a-56250cdee42f",
            "Name": "Projection_Image_Architectural_First Floor_Zone 1.png",
            "Discipline": "Architectural",
            "LevelName": "First Floor",
            "BB": [],
            "SheetFileName": null,
            "ProjectionLineFileId": "36bb6683-c6d3-43c2-bbdc-aedf3203ea86",
            "ProjectionLineFileName": "Projection_Image_Architectural_First Floor_Zone 1.pdf",
            "ZoneName": "Zone 1"
        },
...

即使我尝试添加新的数组成员,此代码也不起作用。是我写作方式的问题吗?

void BimObjectsToProjection::modifyViewBoxForProjection(std::string projectionName, long long minX, long long minY, long long Xlen, long long Ylen)
{
    Value& projections = md_FilesJsonDocument["ProjectionImages"];
    for (Value::ValueIterator projectionsIterator = projections.Begin(); projectionsIterator != projections.End(); ++projectionsIterator)
    {
        rapidjson::Value& projectionJson = *projectionsIterator;
        string name = projectionJson["Name"].GetString();
        if (projectionName == name)
        {
            Document::AllocatorType& allocator = md_FilesJsonDocument.GetAllocator();
           
            Value a(kArrayType);
            a.PushBack((int)minX, allocator);
            a.PushBack((int)minY, allocator);
            a.PushBack((int)Xlen, allocator);
            a.PushBack((int)Ylen, allocator);
            (*projectionsIterator).AddMember("AA", a, allocator);
                  
            rapidjson::StringBuffer strbuf;
            rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
            md_FilesJsonDocument.Accept(writer);
            break;
        }
    }
}

这部分代码没有做任何事情:

rapidjson::StringBuffer strbuf;
rapidjson::Writer<rapidjson::StringBuffer> writer(strbuf);
md_FilesJsonDocument.Accept(writer);

strbuf 包含 json 字符串,但它被丢弃了。我会把它移到一个单独的函数中并用 std::cout << strbuf;.

打印内容

直接写入文件:

std::ofstream ofs("out.json", std::ios::out);
if (ofs.is_open()) {
    rapidjson::OStreamWrapper osw(ofs);
    rapidjson::Writer<rapidjson::OStreamWrapper> writer(osw);
    md_FilesJsonDocument.Accept(writer);
}