JSON的解析器



我需要用qt编辑C 的解析器代码。我受到某人的启发。我遇到了一个问题,他只解析了我的.json文件的一半。

这是我的代码:

    #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();
} 

这是.json代码:

{
    "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"
    }
}

问题在此行中:

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

我需要首先找到":{"背后的ID,然后我将创建一个系统

因为我的代码仅在.json文件中解析" 123"部分。我想知道如何将" 123"one_answers" 456"解析为项目或列表。因为通常我不知道ID是什么。

这可以通过获取所有键来完成,然后您可以在返回的QStringList上使用第一个方法。

有关详细信息,请参见文档。

因此,您会写下这样的东西:

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

您可能会使用开始迭代器以及以下内容:

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

这是用于参考的完整示例项目:

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;
}

构建和运行

qmake && make && ./main

输出

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"]}) )

相关内容

  • 没有找到相关文章

最新更新