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

JSON over socket

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 2.5k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on 22 May 2019, 20:39 last edited by
    #1

    Hello everybody,

    Ok, so, I've had INI file and I've used some functions like childKeys and childGroups to get TAGS,keys and values. After that I've made Json Object that looks like this:

    QJsonObject({"TAG1":[{"name1":"value1"},{"name2":"value2"},...,{"name9":"value9"}],"TAG2":[{"name1":"value1"},{"name2":"value2"},...,{"name9":"value9"}],"TAG3":[{"name1":"value1"},{"name2":"value2"},...,{"name9":"value9"}]})
    

    I've made QJsonDocument and sent it over TCP socket with this code:

    QJsonDocument doc(jsonObj);
    QByteArray jByte(doc.toJson(QJsonDocument::Compact));
    socket->write(jByte);
    

    On the client side I want to reverse parse it so I can have TAGS and values seperately which I'm going to put them in some sort of a table or list. Is there any possible way to do it with some kind of reverse functions of childKeys and childGroups?

    Thanks in advance.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KillerSmath
      wrote on 22 May 2019, 21:10 last edited by KillerSmath
      #2

      @YouKnowMe

      I am not sure if Qt has a default way to convert a json to a childgroup format. But you can read the Json and implement yourself way to create it

      QJsonParseError parseError;
      QJsonDocument doc = QJsonDocument::fromJson(socket->readAll(), &parseError);
      
      if(parseError.error ==  QJsonParseError::NoError){
        // Has a Error
        return;
      }
      
      QJsonObject obj = doc.object();
      
      // acessing all object Keys
      
      for(const QString& key : obj.keys()){
         qDebug() << obj[key];
      }
      

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      ? 1 Reply Last reply 22 May 2019, 21:34
      0
      • F Offline
        F Offline
        fcarney
        wrote on 22 May 2019, 21:16 last edited by
        #3

        @KillerSmath said in JSON over socket:

        childgroup format

        What is meant by childgroup format? I searched online, but I am not seeing a particular concept or pattern.

        C++ is a perfectly valid school of magic.

        K 1 Reply Last reply 22 May 2019, 21:27
        0
        • F fcarney
          22 May 2019, 21:16

          @KillerSmath said in JSON over socket:

          childgroup format

          What is meant by childgroup format? I searched online, but I am not seeing a particular concept or pattern.

          K Offline
          K Offline
          KillerSmath
          wrote on 22 May 2019, 21:27 last edited by
          #4

          @fcarney
          Sorry if I was not clear. I told about the QSettings.
          I am not sure if Qt has a interface to directly convert Json to Ini format.

          @Computer Science Student - Brazil
          Web Developer and Researcher
          “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

          1 Reply Last reply
          0
          • K KillerSmath
            22 May 2019, 21:10

            @YouKnowMe

            I am not sure if Qt has a default way to convert a json to a childgroup format. But you can read the Json and implement yourself way to create it

            QJsonParseError parseError;
            QJsonDocument doc = QJsonDocument::fromJson(socket->readAll(), &parseError);
            
            if(parseError.error ==  QJsonParseError::NoError){
              // Has a Error
              return;
            }
            
            QJsonObject obj = doc.object();
            
            // acessing all object Keys
            
            for(const QString& key : obj.keys()){
               qDebug() << obj[key];
            }
            
            ? Offline
            ? Offline
            A Former User
            wrote on 22 May 2019, 21:34 last edited by
            #5
            This post is deleted!
            K 1 Reply Last reply 22 May 2019, 21:50
            0
            • ? A Former User
              22 May 2019, 21:34

              This post is deleted!

              K Offline
              K Offline
              KillerSmath
              wrote on 22 May 2019, 21:50 last edited by KillerSmath
              #6

              @YouKnowMe
              I noticed some syntax errors in my post. It was already fixed.
              Implicit conversion from list to QJsonDocument ? Can you show a snippet of your code ?

              @Computer Science Student - Brazil
              Web Developer and Researcher
              “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on 22 May 2019, 22:23 last edited by
                #7

                Ok, more details...
                I had an INI file with tags, keys and values, like this:

                [TAG1]
                name1=value1
                name2=value2
                name3=value3
                . . .
                
                [TAG2]
                name1=value1
                name2=value2
                name3=value3
                

                On my server side I got them with this code:

                QSettings settings(":/data.ini",QSettings::IniFormat);
                QStringList groups= settings.childGroups();
                foreach (QString group, groups) {
                      settings.beginGroup(group);
                      QStringList keys = settings.childKeys();
                      QJsonArray arr;
                      foreach (key, keys) {
                               values=settings.value(key);
                               QJsonObject object;
                               QJsonValue valueJson(values.toString());
                               object.insert(key,valueJson);
                               arr.append(object);
                               }
                jsonObj.insert(group,arr);
                settings.endGroup();
                }
                

                As a result I got this Json object:

                QJsonObject({"TAG1":[{"name1":"value1"},{"name2":"value2"},...,{"name9":"value9"}],"TAG2":[{"name1":"value1"},{"name2":"value2"},...,{"name9":"value9"}],"TAG3":[{"name1":"value1"},{"name2":"value2"},...,{"name9":"value9"}]})
                

                Next, I've made QJsonDocument and sent it over TCP socket with the code I wrote in the first post.

                Now on the client side I want to get TAGS and values separately (without name1, name2,...) and put them in a list or a table. Is there a way I can do that?

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  fcarney
                  wrote on 22 May 2019, 22:37 last edited by
                  #8

                  In case you haven't seen this:
                  https://doc.qt.io/qt-5/json.html

                  Anway:

                  QJsonDocument(jstring).object()  // gets your top level QJsonObject
                  

                  From there you are accessing keys inside the object using the keys() call.
                  Your hierarchy should look like this for what you have:
                  QJsonDocument->QJsonObject->QJsonArray->QJsonObject

                  Of course use the tests for isObject, isArray, etc to make sure you have what you think you have.

                  C++ is a perfectly valid school of magic.

                  1 Reply Last reply
                  5

                  1/8

                  22 May 2019, 20:39

                  • Login

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