I have two objects which I wish to compare and display the differences of the values in the two objects. One is my reference object and another is my testing object. The order of the keys, the length of the objects are the same. The integer and float values in my object may slightly vary and I wish to display the difference to the user.
here is the code, and I wish to know if they will iterate through every value in my objects.
if(bool(refobject.length()==testobject.length())==true)
{
QJsonObject::iterator i,j;
for (i = refobject.begin(), j= testobject.begin();
i!=refobject.end()&&j!=testobject.end(); ++i,++j)
{
if(i.key()==j.key())
{
if(i.value().isString() && j.value().isString())
{
if(i.value()==j.value())
{
//display i and j
}
else
{
//display them in a different colour
}
}
else if(i.value().isArray()&& j.value().isArray())
{
//iterate and compare
}
else if(i.value().isObject() && j.value().isObject())
{
//check if there are strings or integers or array and work accordingly
}
else if(i.value().isDouble() && j.value().isDouble())
{
if(i.value()==j.value())
{
//display i and j
}
else
{
//display them in a different colour
}
}
}
}
}
else{//display message that you cannot compare them}
I wish to know if this could help me iterate and compare the values in both objects.
Any other strategies to iterate through two json objects and compare the values would be greatly appreciated!
Thanks in advance!!!
I solved a similar problem: To search only the differences (deleted, created and changed values) and log these. My recursive algorithm works for all structures of JSON Strings. Basically it uses QJsonObject.keys() for Objects and QJsonArray.size() for Arrays for iteration and checks the difference for each value (which by itself is a JsonObject, JsonArray or JsonValue).
JsonComparer.h
JsonComparer.cpp
As a sample output (i had to take some parts out):