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

How to parse such a complex json file



  • How can i parse such a file?
    I have written the reading part but i cant imagine the other parts

    {
        "Plan": [
            {
                "baslangic nokta": 0,
                "rota no": 0,
                "toplam nokta": 3
            },
            {
                "Boylam": 32.7088737487793,
                "Enlem": 40.00210952758789,
                "görev": 0,
                "irtifa": 2500,
                "no": 0,
                "sonraki": 1,
                "yaricap": 0
            },
            {
                "Boylam": 32.715911865234375,
                "Enlem": 39.97725296020508,
                "görev": 0,
                "irtifa": 2500,
                "no": 1,
                "sonraki": 2,
                "yaricap": 0
            },
            {
                "Boylam": 32.74869918823242,
                "Enlem": 40.003292083740234,
                "görev": 0,
                "irtifa": 2500,
                "no": 2,
                "sonraki": 0,
                "yaricap": 0
            }
        ]
    }
    
    ```
    QFile file("/home/test.json");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QByteArray data = file.readAll();
    file.close();
    
    QJsonParseError error;
    QJsonDocument doc = QJsonDocument::fromJson(data, &error);
    if(doc.isNull())
    {
        qDebug() << "parse failed";
    }
    
    
    QJsonObject rootObj = doc.object();
    

  • Lifetime Qt Champion

    Since you create exactly this structure here with the help of Qt's json classes - what's the problem doing it the other way round?



  • @suslucoder said in How to parse such a complex json file:

    How can i parse such a file?

    Parsing is already done by QJsonDocument::fromJson().



  • @KroMignon said in How to parse such a complex json file:

    Parsing is already done by QJsonDocument::fromJson().

    Yes but for using the datas on the file, i want to to such a thing:
    is this the right approach?

    ```
      if(rootObj.contains("Plan"))
        {
           QJsonObject subObj = rootObj.value("Plan").toObject();
           qDebug() << subObj.value("enlem").toString();
           qDebug() << subObj.value("boylam").toString();
    

    }


  • Lifetime Qt Champion

    Simply try it out?



  • @suslucoder said in How to parse such a complex json file:

    is this the right approach?

    Half good!

    With Json, you have to know the structure of the document you want to read.
    Qt gives you some classes to help you:

    • QJsonDocument to read/write a full Json entity
    • QJsonValue: encapsulates a JSON value
    • QJsonObject: encapsulates a JSON object
    • QJsonArray: encapsulates a JSON array

    So you start with parsing the JSON string with QJsonDocument::fromJson(), then get the Root item with:

    • QJsonDocument::toObject() if root is an object (like in your example)
    • QJsonDocument::toArray() if root is an array

    Then go through the structure:
    Your root object has one value called "Plan" with hold an array:
    QJsonArray planArray = root.value("plan").toArray();

    And the got through the array items

    for(const auto &value : planArray )
    {
        QJsonObject object = value.ToObject();
        ...
    }
    

    Hope this helps.



  • @KroMignon It helps me a lot. I read my json file. Thank you so much



  • @KroMignon Hi. In here, how can i reach each element in the array. Since i have 2 root in my planArray, i want to achieve them differently.



  • @suslucoder said in How to parse such a complex json file:

    Since i have 2 root in my planArray, i want to achieve them differently.

    I don't understand this?!?

    A JSON document can only have one root element: an object (which can have multiple values) or an array (which can have multiple objects).



  • @suslucoder The "Plan" array is a little unusual because the objects it contains are not all of the same structure. Usually, arrays should contain a sequence of elements of the same type. However, it is legal JSON.

    If you are certain that the first element of the "Plan" array will have the structure as in your example above, you can perhaps rely on the others to all have the 2nd type structure. Otherwise, you might need to check for the existence of certain keys -- for example, by using the function QJsonObject::contains() to determine which kind of object it is.


Log in to reply