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. Parse multiple Json
Qt 6.11 is out! See what's new in the release blog

Parse multiple Json

Scheduled Pinned Locked Moved General and Desktop
json
4 Posts 2 Posters 3.0k Views 3 Watching
  • 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.
  • K Offline
    K Offline
    kDohmen
    wrote on last edited by kDohmen
    #1

    Hello,

    I have the same question as in this thread. But I think it is not an option to count the {}brackets.
    At the moment I do something like this:

    QByteArray json_data;
    QJsonDocument doc(QJsonDocument::fromJson(json_data));
    json_data.remove(0, doc.toJson().length());
    

    The problem here is that I don't know the format of the incoming Json. This I try to evaluate with:
    json_data.startsWith("{\"");

    This way is not very good and I hope there is a bettery way to accomplish to parse multiple jsons.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      in the thread you posted the example text is not a valid JSON.
      What is the source of these data?

      BTW, your solution should work

      QByteArray json_data;
      QList<QJSonDocument> docs;
      
      while(!json_data.isEmpty()) {
        QJsonDocument doc(QJsonDocument::fromJson(json_data));
        json_data.remove(0, doc.toJson().length());
        docs.push_back(doc);
      }
      

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kDohmen
        wrote on last edited by kDohmen
        #3

        an other application sends me the data via a tcpconntection. but this application sends many json at one time. when I use QTcpSocket.readAll() I get one long string with many Jsons. so I need a way to devorce this string in many little jsons.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          Hi,

          usually in TCP is a best practice to do one of the following:

          • Send a header with some sync info (special values, length, ...)
          • Using New-line for text information

          Are you sure there's no special character between JSON objects?
          Keep in mind that

           QJsonDocument doc(QJsonDocument::fromJson(json_data));
           json_data.remove(0, doc.toJson().length());
          

          could not work because QJsonDocument, make some formatting on the string so doc.toJSon().length() could return a value that is not the same you read.

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0

          • Login

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