2

I am using jsoncpp, I have a set of data as below, I want to search some node and remove the array element, some how when I remove, it become {}, is that possible to completely remove with {}?

Data before remove:

{
    "any" : [
    {
        "any_id" : "a1",
        "infos" : [
        {
            "info_id" : "i1",
            "path" : "",
            "version" : "1.0"
        }
        ]
    },
    {
        "any_id" : "a2",
        "infos" : [
        {
            "info_id" : "i1",
            "path" : "",
            "version" : "1.0"
        },
        {
            "info_id" : "i2",
            "path" : "D:\\",
            "version" : "1.0"
        },
        {
            "info_id" : "i3",
            "path" : "",
            "version" : "1.0"
        },
        {
            "info_id" : "i4",
            "path" : "D:\\",
            "version" : "1.0"
        }
        ]
    },
    {
        "any_id" : "a3",
        "infos" : [
        {
            "info_id" : "i4",
            "path" : "",
            "version" : "1.0"
        }
        ]
    }
    ]
}

Data after remove:

{
    "any" : [
    {
        "any_id" : "a1",
        "infos" : [
        {
            "info_id" : "i1",
            "path" : "",
            "version" : "1.0"
        }
        ]
    },
    {
        "any_id" : "a2",
        "infos" : [
        {
            "info_id" : "i1",
            "path" : "",
            "version" : "1.0"
        },
        {
        },
        {
            "info_id" : "i3",
            "path" : "",
            "version" : "1.0"
        },
        {
        }
        ]
    },
    {
        "any_id" : "a3",
        "infos" : [
        {
            "info_id" : "i4",
            "path" : "",
            "version" : "1.0"
        }
        ]
    }
    ]
}

in C++, I have the following code, look for any_id and path to remove array element:

for (Json::ValueIterator &itr_any = data["any"].begin();
        itr_any != data["any"].end(); ++itr_any)
    {
        if (0 == _stricmp((*itr_any)["any_id"].asString().c_str(), "a2"))
        {
            for (Json::ValueIterator &itr_res = (*itr_any)["infos"].begin();
                itr_res != (*itr_any)["infos"].end(); ++itr_res)
            {
                if (0 == _stricmp((*itr_res)["path"].asString().c_str(), "D:\\"))
                {
                    (*itr_res).clear();
                }
            }
        }
    }

1 Answer 1

2

With (*itr_res).clear(); you are in fact emptying the content of the object in the chosen array.

JsonCpp APIs don't expose methods to manipulate arrays, so the easiest way to remove items is to build a new array excluding matching elements:

for (Json::ValueIterator &itr_any = data["any"].begin(); itr_any != data["any"].end(); ++itr_any)
    if (0 == _stricmp((*itr_any)["any_id"].asString().c_str(), "a2"))
    {
        Json::Value newArray = Json::arrayValue;
        for (Json::ValueIterator &itr_res = (*itr_any)["infos"].begin(); itr_res != (*itr_any)["infos"].end(); ++itr_res)
            if (_stricmp((*itr_res)["path"].asString().c_str(), "D:\\"))
                newArray.append((*itr_res));
        (*itr_any)["infos"] = newArray;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.