Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. QJsonObject comparison

QJsonObject comparison

Scheduled Pinned Locked Moved Unsolved Brainstorm
3 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rosh7
    wrote on last edited by aha_1980
    #1

    So, I have two objects of same length and order and I want to compare the values in my object using keys. I have written a rough code and I would like to know if it works or is there any other way that this could be done.
    I cannot use the qjsonobject equality method because even though the order and the keys are the same, the values might vary and I wish to point that difference out by iterating through 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}
    }
    }
    
    JonBJ 1 Reply Last reply
    0
    • R rosh7

      So, I have two objects of same length and order and I want to compare the values in my object using keys. I have written a rough code and I would like to know if it works or is there any other way that this could be done.
      I cannot use the qjsonobject equality method because even though the order and the keys are the same, the values might vary and I wish to point that difference out by iterating through 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}
      }
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @rosh7
      In principle your iteration algorithm looks OK. Since you do not alter either object you could use https://doc.qt.io/qt-5/qjsonobject-const-iterator.html instead of QJsonObject::iterator, to maybe gain an extra mark :)

      In your isObject() case, and also in your isArray() case where you note you will need to iterate and compare, you will need to call this function again recursively if you encounter an isObject() (i.e. a QJsonObject). In your other post, you showed your input file, and IIRC it had JSON objects and/or arrays at the top level(s), not only at the bottom, leaf levels. So the recursion will be needed to walk down the tree.

      R 1 Reply Last reply
      1
      • JonBJ JonB

        @rosh7
        In principle your iteration algorithm looks OK. Since you do not alter either object you could use https://doc.qt.io/qt-5/qjsonobject-const-iterator.html instead of QJsonObject::iterator, to maybe gain an extra mark :)

        In your isObject() case, and also in your isArray() case where you note you will need to iterate and compare, you will need to call this function again recursively if you encounter an isObject() (i.e. a QJsonObject). In your other post, you showed your input file, and IIRC it had JSON objects and/or arrays at the top level(s), not only at the bottom, leaf levels. So the recursion will be needed to walk down the tree.

        R Offline
        R Offline
        rosh7
        wrote on last edited by
        #3

        @JonB
        Thanks a lot for your insight!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved