Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to parse a json file without a key?

How to parse a json file without a key?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dennis.kim
    wrote on 22 Mar 2022, 14:33 last edited by
    #1

    Hi,
    the file i want to parse is as follows.

    [
    3,
    "aabfff",
    {
    "currentTime": "2022-03-22T14:21:17.416524",
    "interval":10,
    "status":"Accepted"
    }
    ]

    i would like to get each item by index if possible.
    I don't know how to parse it.
    In particular, I have no idea how to get <3, "aabfff"> without a Key value.
    If you know how, please advise.

    J A P 3 Replies Last reply 22 Mar 2022, 14:40
    0
    • D dennis.kim
      22 Mar 2022, 14:33

      Hi,
      the file i want to parse is as follows.

      [
      3,
      "aabfff",
      {
      "currentTime": "2022-03-22T14:21:17.416524",
      "interval":10,
      "status":"Accepted"
      }
      ]

      i would like to get each item by index if possible.
      I don't know how to parse it.
      In particular, I have no idea how to get <3, "aabfff"> without a Key value.
      If you know how, please advise.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 22 Mar 2022, 14:40 last edited by
      #2

      @dennis-kim said in How to parse a json file without a key?:

      I have no idea how to get <3, "aabfff"> without a Key value

      Using index. You have an array, so use index.
      Read your JSON into QJsonDocument using https://doc.qt.io/qt-5/qjsondocument.html#fromJson
      Then convert QJsonDocument to array using https://doc.qt.io/qt-5/qjsondocument.html#array
      Then access array elements using https://doc.qt.io/qt-5/qjsonarray.html#operator-5b-5d

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply 23 Mar 2022, 01:41
      4
      • D dennis.kim
        22 Mar 2022, 14:33

        Hi,
        the file i want to parse is as follows.

        [
        3,
        "aabfff",
        {
        "currentTime": "2022-03-22T14:21:17.416524",
        "interval":10,
        "status":"Accepted"
        }
        ]

        i would like to get each item by index if possible.
        I don't know how to parse it.
        In particular, I have no idea how to get <3, "aabfff"> without a Key value.
        If you know how, please advise.

        A Offline
        A Offline
        artwaw
        wrote on 22 Mar 2022, 14:40 last edited by
        #3

        @dennis-kim the top level is QJsonArray, you can just iterate over it, can't you?

        PS. @jsulm was faster ;)

        For more information please re-read.

        Kind Regards,
        Artur

        1 Reply Last reply
        4
        • D dennis.kim
          22 Mar 2022, 14:33

          Hi,
          the file i want to parse is as follows.

          [
          3,
          "aabfff",
          {
          "currentTime": "2022-03-22T14:21:17.416524",
          "interval":10,
          "status":"Accepted"
          }
          ]

          i would like to get each item by index if possible.
          I don't know how to parse it.
          In particular, I have no idea how to get <3, "aabfff"> without a Key value.
          If you know how, please advise.

          P Offline
          P Offline
          Pl45m4
          wrote on 22 Mar 2022, 14:48 last edited by Pl45m4
          #4

          @dennis-kim

          The text representation of JSON encloses arrays in square brackets ([ ... ]) and objects in curly brackets ({ ... }). Entries in arrays and objects are separated by commas. The separator between keys and values in an object is a colon (:).

          (https://doc.qt.io/qt-5/json.html)

          So, what you have here is a JSON, which contains an array of ints, strings and json objects...

          Edit:
          LOL, didn't refresh my page... thought, I'd be the first :) sry @jsulm @artwaw :D


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          4
          • J jsulm
            22 Mar 2022, 14:40

            @dennis-kim said in How to parse a json file without a key?:

            I have no idea how to get <3, "aabfff"> without a Key value

            Using index. You have an array, so use index.
            Read your JSON into QJsonDocument using https://doc.qt.io/qt-5/qjsondocument.html#fromJson
            Then convert QJsonDocument to array using https://doc.qt.io/qt-5/qjsondocument.html#array
            Then access array elements using https://doc.qt.io/qt-5/qjsonarray.html#operator-5b-5d

            D Offline
            D Offline
            dennis.kim
            wrote on 23 Mar 2022, 01:41 last edited by
            #5

            @jsulm

            thanks a lot...
            Solved this issue.

                **QByteArray json_bytes = json_msg->toLocal8Bit();
                QJsonDocument doc = QJsonDocument::fromJson(json_bytes, &err);
                QJsonArray jsonArray = doc.array();
                QJsonValue abc = jsonArray[0];
                qDebug() << "abc:" << abc << "type : " << abc.type();
            
                abc = jsonArray[1];
                qDebug() << "abc1:" << abc << "type : " << abc.type();
            
                abc = jsonArray[2];
                qDebug() << "abc2:" << abc << "type : " << abc.type();
            
                for (auto it = jsonArray.constBegin(); it != jsonArray.constEnd(); ++it)
                {
                    const QJsonValue &val = *it;
            
                    QJsonObject o = val.toObject();
                    for (auto oIt = o.constBegin(); oIt != o.constEnd(); ++oIt)
                    {
                        qDebug() << "Key:" << oIt.key() << ", Value:" << oIt.value().toString();
                    }
                }**
            
            1 Reply Last reply
            2

            1/5

            22 Mar 2022, 14:33

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved