Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

JSON over socket



  • 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.



  • @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];
    }
    


  • @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.



  • @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.



  • This post is deleted!


  • @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 ?



  • 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?



  • 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.


Log in to reply