2

我有一个路径列表,例如:"blabla/bleble/blibli", "blabla/blibli/bleble" 我已经成功地将一个路径转换为:{"blabla":{"bleble":{"blibli":null}}}使用此功能:

nlohmann::json transform(std::pair<std::string, int> aaa){
    nlohmann::json pathJsoned = {};
    auto splitedPath = split(aaa.first, '/');
    nlohmann::json* bla = &pathJsoned;

    for(int i = 0; i < splitedPath.size(); i++){
        if( !bla->is_null() && bla->at(splitedPath[i].c_str()).is_object() ){
            if(i == splitedPath.size()-1){
                std::cout << i << " || " << splitedPath.size() << std::endl;
                pathJsoned[splitedPath[i].c_str()] = "FILE";
            }else{
                std::cout << i << " || " << splitedPath.size() << std::endl;
                pathJsoned[splitedPath[i].c_str()] = nlohmann::json();
            }
        }
        nlohmann::json* a = &bla->operator[](splitedPath[i].c_str());
        bla = a;
    }
    return pathJsoned;
}

但现在我想合并它,它不起作用。我试过了:

nlohmann::json merge( const nlohmann::json &a, const nlohmann::json &b )
{
    nlohmann::json result = a.flatten();
    nlohmann::json tmp = b.flatten();
    for ( auto it = tmp.begin(); it != tmp.end(); ++it )
        result[it.key()] = it.value();
    return result.unflatten();
}

结果没用,就像合并什么都没做一样,我尝试了 nlohmann::json 库的 merge_patch 方法,但它完成了一半的工作:

{
    "blabla": {
        "bleble": {
            "blibli": null
        },
        "blibli": {}
    }
}

预期结果:

{
    "blabla": {
        "bleble": {
            "blibli": null
        },
        "blibli": {
            "bleble": null
        }
    }
}

你知道是否可以合并完整的 json 吗?

这是整个代码:https ://pastebin.com/Qyc8EQN0

4

0 回答 0