Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [HEHLP] Get values from object inside object in a Json file
Forum Updated to NodeBB v4.3 + New Features

[HEHLP] Get values from object inside object in a Json file

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 536 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.
  • ElmehdiE Offline
    ElmehdiE Offline
    Elmehdi
    wrote on last edited by Elmehdi
    #1

    Hello, I have a Json file like this :

        {"country":"Morocco",
        
            "province":["mainland"],
        
            "timeline":{"cases":{"5/24/20":7433,
        
            "5/25/20":7532,
        
            "5/26/20":7577,
        
            "6/22/20":10172},
        
            "deaths":{"5/24/20":199,
        
            "5/25/20":200,
        
            "5/26/20":202,
        
            "6/22/20":214},
        
            "recovered":{"5/24/20":4703,
        
            "5/25/20":4774,
        
            "5/26/20":4881,
        
            "6/22/20":8366}}}
    

    And I want to store the values that are in front of the dates in a Vector; for example I have | "6/22/20":8366 | I want to store 8366 in the vector. I'm really not good with the parsing of the Json files. This is what I did but didn't work, what have I done wrong or what is the correct way to do it?

    QJsonObject j_obj = json_doc.object();
    QJsonValue val, inner_val;
     QVector<double> y(7);
           
     for(auto jsonObj : j_obj)
            {
               val = jsonObj.toObject().value("cases");
               if(!val.isUndefined())
                {
                   for (auto jsobj : jsonObj.toObject())
                   {
                       inner_val = jsobj.toObject().value("");
                       y[i] = inner_val.toInt();
                       qDebug() << y[i];
                       i++;
                   }
                }
               val = 0;
            }
    jsulmJ 1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by
      #6
      QJsonObject timeline = j_obj["timeline"].toObject();
      QJsonObject cases = timeline["cases"].toObject();
      for (auto jsobj : cases)
      {
          y[i] = jsobj.toInt();
          qDebug() << "GOOOOOOOOOOOODD" << y[i];
          i++;
      }
      
      1 Reply Last reply
      3
      • ElmehdiE Elmehdi

        Hello, I have a Json file like this :

            {"country":"Morocco",
            
                "province":["mainland"],
            
                "timeline":{"cases":{"5/24/20":7433,
            
                "5/25/20":7532,
            
                "5/26/20":7577,
            
                "6/22/20":10172},
            
                "deaths":{"5/24/20":199,
            
                "5/25/20":200,
            
                "5/26/20":202,
            
                "6/22/20":214},
            
                "recovered":{"5/24/20":4703,
            
                "5/25/20":4774,
            
                "5/26/20":4881,
            
                "6/22/20":8366}}}
        

        And I want to store the values that are in front of the dates in a Vector; for example I have | "6/22/20":8366 | I want to store 8366 in the vector. I'm really not good with the parsing of the Json files. This is what I did but didn't work, what have I done wrong or what is the correct way to do it?

        QJsonObject j_obj = json_doc.object();
        QJsonValue val, inner_val;
         QVector<double> y(7);
               
         for(auto jsonObj : j_obj)
                {
                   val = jsonObj.toObject().value("cases");
                   if(!val.isUndefined())
                    {
                       for (auto jsobj : jsonObj.toObject())
                       {
                           inner_val = jsobj.toObject().value("");
                           y[i] = inner_val.toInt();
                           qDebug() << y[i];
                           i++;
                       }
                    }
                   val = 0;
                }
        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Elmehdi said in [HEHLP] Get values from object inside object in a Json file:

        "cases"

        cases is inside "timeline", so you first need to search for "timeline"...

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • ElmehdiE Offline
          ElmehdiE Offline
          Elmehdi
          wrote on last edited by
          #3

          @jsulm Hello!

          you said I should search for the timeline then search for cases. Can you help me about that, this is what I did but doesn't seem to work

          QJsonObject timeline = j_obj["timeline"].toObject();
          for(auto jsonObj : timeline)
          {
          qDebug() << "aisaiisisaiasiasiass" << y[i];
          QJsonObject cases = timeline["cases"].toObject();
           for (auto jsobj : cases)
            {
              inner_val = jsobj.toObject().value("5/24/20");
              y[i] = inner_val.toInt();
             qDebug() << "GOOOOOOOOOOOODD" << y[i];
             i++;
            }
          }
          
          JonBJ 1 Reply Last reply
          0
          • ElmehdiE Elmehdi

            @jsulm Hello!

            you said I should search for the timeline then search for cases. Can you help me about that, this is what I did but doesn't seem to work

            QJsonObject timeline = j_obj["timeline"].toObject();
            for(auto jsonObj : timeline)
            {
            qDebug() << "aisaiisisaiasiasiass" << y[i];
            QJsonObject cases = timeline["cases"].toObject();
             for (auto jsobj : cases)
              {
                inner_val = jsobj.toObject().value("5/24/20");
                y[i] = inner_val.toInt();
               qDebug() << "GOOOOOOOOOOOODD" << y[i];
               i++;
              }
            }
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @Elmehdi
            It looks about right, except that obviously it would only ever match the one "5/24/20" key.

            but doesn't seem to work

            You should say what "doesn't seem to work" means. I see some qDebug(), and you could probably do with some more, you should say whether they are hit, what the output is, so that we can match to the input.

            1 Reply Last reply
            0
            • ElmehdiE Offline
              ElmehdiE Offline
              Elmehdi
              wrote on last edited by
              #5

              @JonB Sorry I don't understand you well,

              • obviously it would only ever match the one "5/24/20" key
                But it never prints the value of it.
                Apparently it hit them all since the qDebug is called exactly 4 times the number of the dates but it didn't print the value.
              1 Reply Last reply
              0
              • B Offline
                B Offline
                Bonnie
                wrote on last edited by
                #6
                QJsonObject timeline = j_obj["timeline"].toObject();
                QJsonObject cases = timeline["cases"].toObject();
                for (auto jsobj : cases)
                {
                    y[i] = jsobj.toInt();
                    qDebug() << "GOOOOOOOOOOOODD" << y[i];
                    i++;
                }
                
                1 Reply Last reply
                3
                • ElmehdiE Offline
                  ElmehdiE Offline
                  Elmehdi
                  wrote on last edited by
                  #7

                  @Bonnie Thank you verry much my dear friend that was exactly what I'm looking for.

                  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