添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
帅气的松球  ·  JSON_ARRAYAGG | ...·  1小时前    · 
逆袭的生菜  ·  JSON_ARRAYAGG ...·  1小时前    · 
性感的小虾米  ·  Convert char array to ...·  17 小时前    · 
发呆的煎鸡蛋  ·  [RESOLVED] how to ...·  昨天    · 
爱玩的小笼包  ·  How to parse such a ...·  昨天    · 
坏坏的大海  ·  Signs Australia is ...·  2 月前    · 
至今单身的青蛙  ·  基于.net C# Socket ...·  9 月前    · 
痴情的啄木鸟  ·  sinatra是什么意思 ...·  1 年前    · 
Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode . Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).

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

Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
Visit the Qt Academy at https://academy.qt.io/catalog

@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();

Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
Visit the Qt Academy at https://academy.qt.io/catalog

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.

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