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": "[email protected]",
"to": [
"[email protected]",
"[email protected]"
],
"cc": [
"[email protected]",
"[email protected]"
],
"cci": [
"[email protected]"
],
"obj": "trolol > all",
"contenu": "Hello 3301 Word ! !",
"date": "21-1-1993",
"domaine": "antartica-base.com",
"state": "0",
"pseudo_f": "Jd"
},
"456": {
"from": "[email protected]",
"to": [
"[email protected]",
"[email protected]"
],
"cc": [
"[email protected]",
"[email protected]"
],
"cci": [
"[email protected]"
],
"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.
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:
{
"123": {
"from": "[email protected]",
"to": [
"[email protected]",
"[email protected]"
],
"cc": [
"[email protected]",
"[email protected]"
],
"cci": [
"[email protected]"
],
"obj": "EpiMTP2017 > all",
"contenu": "Hello 3301 Word ! !",
"date": "21-12-1993",
"domaine": "antartica-base.com",
"state": "0",
"pseudo_f": "Jd"
},
"456": {
"from": "[email protected]",
"to": [
"[email protected]",
"[email protected]"
],
"cc": [
"[email protected]",
"[email protected]"
],
"cci": [
"[email protected]"
],
"obj": "EpiMTP2017 > all",
"contenu": "Hello 3301 Word ! !",
"date": "21-12-1993",
"domaine": "antartica-base.com",
"state": "0",
"pseudo_f": "Jd"
}
}
TEMPLATE = app
TARGET = main
QT = core
SOURCES += 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;
}
qmake && make && ./main
QJsonValue(object, QJsonObject({"cc": ["[email protected]","[email protected]"],"cci": ["[email protected]"],"contenu": "Hello 3301 Word ! !","date": "21-12-1993","domaine": "antartica-base.com","from": "[email protected]","obj": "EpiMTP2017 > all","pseudo_f": "Jd","state": "0","to": ["[email protected]","[email protected]"]}) )
===========
QJsonValue(object, QJsonObject({"cc": ["[email protected]","[email protected]"],"cci": ["[email protected]"],"contenu": "Hello 3301 Word ! !","date": "21-12-1993","domaine": "antartica-base.com","from": "[email protected]","obj": "EpiMTP2017 > all","pseudo_f": "Jd","state": "0","to": ["[email protected]","[email protected]"]}) )