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. Json iteration
Forum Updated to NodeBB v4.3 + New Features

Json iteration

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 271 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
    RoachMan
    wrote on last edited by
    #1

    Hi
    I am new to json with Qt and am having some difficulty traversing my Json file.

    {
      "SEMCount": "2",
      "StringDef": {
        "SEMS": {
          "SEM": {
            "id": "0001",
            "ADCGain": "10"
          },
    	  "SEM": {
            "id": "0002",
            "ADCGain": "20"
          }
        }
      }
    }
    I have taken this approach, but it only loops over the first 2 elements.
    

    for (QJsonObject::const_iterator it = doc.Root().constBegin();
    it != doc.Root().constEnd();
    it++)
    {
    }

    ``Any help is appreciated
    JonBJ 1 Reply Last reply
    0
    • R RoachMan

      Hi
      I am new to json with Qt and am having some difficulty traversing my Json file.

      {
        "SEMCount": "2",
        "StringDef": {
          "SEMS": {
            "SEM": {
              "id": "0001",
              "ADCGain": "10"
            },
      	  "SEM": {
              "id": "0002",
              "ADCGain": "20"
            }
          }
        }
      }
      I have taken this approach, but it only loops over the first 2 elements.
      

      for (QJsonObject::const_iterator it = doc.Root().constBegin();
      it != doc.Root().constEnd();
      it++)
      {
      }

      ``Any help is appreciated
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @RoachMan
      Umm, well, there are indeed only 2 top-level elements in your document: SEMCount and StringDef. So it is doing exactly what it should do! What else did you expect? If you mean you want to descend the tree to visit every element then you are going to want to recurse on each element encountered. As you meet each QJsonValue you can examine its type via various is...() methods there and/or type() will return an enum QJsonValue::Type to tell you. If it's a QJsonValue::Object (as it is for your StringDef) you need to recurse into it in the same way.

      R 1 Reply Last reply
      4
      • JonBJ JonB

        @RoachMan
        Umm, well, there are indeed only 2 top-level elements in your document: SEMCount and StringDef. So it is doing exactly what it should do! What else did you expect? If you mean you want to descend the tree to visit every element then you are going to want to recurse on each element encountered. As you meet each QJsonValue you can examine its type via various is...() methods there and/or type() will return an enum QJsonValue::Type to tell you. If it's a QJsonValue::Object (as it is for your StringDef) you need to recurse into it in the same way.

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

        @JonB Thanks for the hints

        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