Question

I need to edit my parser code in C++ with QT. I got inspired by someone. I got an issue, he parses only the half of my .json file.

this is my code :

    #include <QJsonArray>
    #include "mainwindow.h"
    #include <QApplication>
    #include <stdio.h>
    #include <QPushButton>
    #include <QFile>
    #include <QDebug>
    #include <QJsonDocument>
    #include <QJsonObject>
    #include <QTranslator>
    #include "parcer_js.h"
    #include <iostream>
    #include <QList>
    #include <QString>
    #include <string>
    #include <parcer_js.h>
    #include <Mail.hpp>
    #include <QDate>

void Parcer::parcerJs()
{
      QString val;
      QFile file;
      file.setFileName("jstest.json");

      file.open(QIODevice::ReadOnly | QIODevice::Text);
      val = file.readAll();
      file.close();

      QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
      ParcerByfile(d);
}

void Parcer::ParcerByfile(QJsonDocument d)
{
      QJsonObject sett2 = d.object();
      qWarning() << sett2;

      QJsonValue value = sett2.value(QString("123")); // Problem !!! 
      QJsonObject item = value.toObject();

      QJsonValue subFrom = item["from"];
      qWarning() << subFrom.toString();

      QJsonArray strTo = item["to"].toArray();
      qWarning() << strTo[0].toString();
      qWarning() << strTo[1].toString();
} 

This is the .json code :

{
    "123": {
        "from": "magellan.dunord@antarctica.com",
        "to": [
            "lol@hotmail.fr",
            "pipo@antartica-base.com"
        ],
        "cc": [
            "johndoe@il2.fr",
            "gordonfreeman@antarctica.com"
        ],
        "cci": [
            "missepitech@antarctica.com"
        ],
        "obj": "trolol > all",
        "contenu": "Hello  3301  Word ! !",
        "date": "21-1-1993",
        "domaine": "antartica-base.com",
        "state": "0",
        "pseudo_f": "Jd"
    },
    "456": {
        "from": "pipo@antarctica.com",
        "to": [
            "lol@hotmail.fr",
            "pipo@antartica-base.com"
        ],
        "cc": [
            "johndoe@flavicon.fr",
            "gordonfreeman@qwerty.fr"
        ],
        "cci": [
            "missepitech@antarctica.com"
        ],
        "obj": "3301 > all",
        "contenu": "Hello 3301 Word ! !",
        "date": "21-12-1993",
        "domaine": "antartica-base.com",
        "state": "0",
        "pseudo_f": "Jd"
    }
}

The problem is in this line :

QJsonValue value = sett2.value(QString("123"));

I need to find first the ID behind this ": {" and I will create a system

because my code only parses "123" part in my .json file. I want to know how I can parse "123" and "456" into an item or a list. Because normally I do not know what would be the ID.

Était-ce utile?

La solution

This can be done by obtaining all the keys and then you can use the first method on the returned QStringList.

See the documentation for details.

Therefore, you would be writing something like this:

QJsonValue value = sett2.value(sett2.keys().first()); // NO Problem !!!

You could likely use the begin iterator as well as follows:

QJsonValue value = sett2.begin().value();

Here is the full sample project for reference:

main.json

{
    "123": {
        "from": "magellan.dunord@antartica-base.com",
        "to": [
            "lol@hotmail.fr",
            "pipo@antartica-base.com"
        ],
        "cc": [
            "johndoe@antartica-base.com",
            "gordonfreeman@antartica-base.com"
        ],
        "cci": [
            "missepitech@antartica-base.com"
        ],
        "obj": "EpiMTP2017 > all",
        "contenu": "Hello  3301  Word ! !",
        "date": "21-12-1993",
        "domaine": "antartica-base.com",
        "state": "0",
        "pseudo_f": "Jd"
    },
    "456": {
        "from": "laray.croft@antartica-base.com",
        "to": [
            "lol@hotmail.fr",
            "pipo@antartica-base.com"
        ],
        "cc": [
            "johndoe@antartica-base.com",
            "gordonfreeman@antartica-base.com"
        ],
        "cci": [
            "missepitech@antartica-base.com"
        ],
        "obj": "EpiMTP2017 > all",
        "contenu": "Hello 3301 Word ! !",
        "date": "21-12-1993",
        "domaine": "antartica-base.com",
        "state": "0",
        "pseudo_f": "Jd"
    }
}

main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp

main.cpp

#include <QFile>
#include <QByteArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QDebug>

int main()
{
    QFile file("main.json");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QByteArray jsonData = file.readAll();
    file.close();

    QJsonDocument d = QJsonDocument::fromJson(jsonData);
    QJsonObject sett2 = d.object();

    QJsonValue value = sett2.begin().value();
    QJsonValue value2 = sett2.value(sett2.keys().first());
    qDebug() << value;
    qDebug() << " =========== ";
    qDebug() << value2;
    return 0;
}

Build and Run

qmake && make && ./main

Output

QJsonValue(object, QJsonObject({"cc": ["johndoe@antartica-base.com","gordonfreeman@antartica-base.com"],"cci": ["missepitech@antartica-base.com"],"contenu": "Hello  3301  Word ! !","date": "21-12-1993","domaine": "antartica-base.com","from": "magellan.dunord@antartica-base.com","obj": "EpiMTP2017 > all","pseudo_f": "Jd","state": "0","to": ["lol@hotmail.fr","pipo@antartica-base.com"]}) ) 
===========  
QJsonValue(object, QJsonObject({"cc": ["johndoe@antartica-base.com","gordonfreeman@antartica-base.com"],"cci": ["missepitech@antartica-base.com"],"contenu": "Hello  3301  Word ! !","date": "21-12-1993","domaine": "antartica-base.com","from": "magellan.dunord@antartica-base.com","obj": "EpiMTP2017 > all","pseudo_f": "Jd","state": "0","to": ["lol@hotmail.fr","pipo@antartica-base.com"]}) )
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top