[Solved] jsoncpp loop deletes data that meets the conditions

This blog post introduces how to delete data that meets the conditions based on the operation of json data under the jsoncpp library.

problem origin

I want to use jsoncpp to delete the contents of the json file in a loop.

problem solution

Use the function removeIndex of jsoncpp


Json::Value del;
testcases.removeIndex(1,&del);

The meaning of this sentence is to delete the content whose index is 1, and pass the deleted data to the del value. What if it loops. '
If the json file has

 "TestCases" : [
               {
    
    
                  "Enable" : true
               },
               {
    
    
                  "Enable" : true
               },
               {
    
    
                  "Enable" : true
               },
               {
    
    
                  "Enable" : false
               }
            ]

If I want to delete the content whose enable is false, then I can write it like this and it will become a circular deletion.

Json::Value& testCases = root["TestCases"];
Json::Value del;
for(Json::ArrayIndex i = 0;i< testCases.size();i++){
    
    
	if(!testCases[i]["Enable"].asBool()){
    
    
		testCases.removeIndex(i,&del);
		i--;
	}

}

Guess you like

Origin blog.csdn.net/m0_37149062/article/details/131130826