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. Load JSON file
Forum Updated to NodeBB v4.3 + New Features

Load JSON file

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 11.2k Views 2 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.
  • N Offline
    N Offline
    neda
    wrote on 11 Nov 2018, 06:14 last edited by
    #1

    Hi,
    I have been trying to read a JSON file.
    But this code does not work.

    JSON file is the best choice for save points? (Reading speed is very important)
    Please guide me.
    Thanks

    file:

    {
         "point":{
               "x":1.12
               "y":1.19
             },
         "point":{
               "x":1.15
               "y":1.22
             },
    ....
    ....
    }
    
    QVector<QPointF> listPoints;    
        QJsonObject json=loadJson(fileName);
        QJsonValue value=json.value("point");
        QJsonArray array=value.toArray();
        foreach(const QJsonValue & val,array){
            double x=val.toObject().value("x").toDouble();
            double y=val.toObject().value("y").toDouble();
            listPoints.append(QPointF(x,y));        
        }
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 11 Nov 2018, 14:06 last edited by mrjj 11 Nov 2018, 14:16
      #2

      Hi
      if reading speed is of great concern, a binary file is faster.
      Where does points come from. Can you change how its saved ?
      That said, how many points re we talking about ?

      N 1 Reply Last reply 12 Nov 2018, 04:53
      2
      • M Offline
        M Offline
        mranger90
        wrote on 11 Nov 2018, 23:55 last edited by
        #3

        First, as @mrjj stated, if there are a lot of points, then you should consider other means besides json.
        As for your current status, the json is ill-formed: you should have something like:

        {
            "points" : [
                {
                     "x" : 1.12,
                     "y" : 1.19
                 },
                {
                     "x" : 1.15,
                     "y" : 1.22
                 },
                {
                     "x" : 1.18,
                     "y" : 1.25
                 }
            ]
        }
        

        Code to parse it is something like:

            QFile inFile("/home/joem/tmp/mypoints.json");
            inFile.open(QIODevice::ReadOnly|QIODevice::Text);
            QByteArray data = inFile.readAll();
            inFile.close();
        
            QJsonParseError errorPtr;
            QJsonDocument doc = QJsonDocument::fromJson(data, &errorPtr);
            if (doc.isNull()) {
                qDebug() << "Parse failed";
            }
            QJsonObject rootObj = doc.object();
            QVector<QPointF> listPoints;
            QJsonArray ptsArray = rootObj.value("points").toArray();
            qDebug() << "There are " << ptsArray.size() << "sets of points in the json file";
            foreach(const QJsonValue & val, ptsArray){
                double x=val.toObject().value("x").toDouble();
                double y=val.toObject().value("y").toDouble();
                listPoints.append(QPointF(x,y));
            }
        
            qDebug() << "Finished parsing, heres the data";
            for(auto pt: listPoints) {
                qDebug() << pt.x() << "," << pt.y();
            }
        
        1 Reply Last reply
        11
        • M mrjj
          11 Nov 2018, 14:06

          Hi
          if reading speed is of great concern, a binary file is faster.
          Where does points come from. Can you change how its saved ?
          That said, how many points re we talking about ?

          N Offline
          N Offline
          neda
          wrote on 12 Nov 2018, 04:53 last edited by neda 11 Dec 2018, 05:01
          #4

          @mrjj said in Load JSON file:

          Where does points come from.

          Thanks for your reply.
          I read data from "serialPort" and save as point in "QVector<QPointF>".
          "QVector<QPointF>" is the best choice for hold points? (I have a lot of points (more than 1,000,000 points) and a lot of processes on points)
          I use these points for draw real time chart (QML).

          Can you change how its saved ?

          What do you suggest?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 12 Nov 2018, 07:19 last edited by mrjj 11 Dec 2018, 07:23
            #5

            Hi

            Like this ( test code)
            I would test the speed of both since @mranger90 was so kind to provide the json sample.

                auto const s = 10000000L;
                auto const path = QStringLiteral("/path/to/file");
                
                    QVector<int> x;
                    x.resize(s);
            // save
                    QFile f(path);
                    if (!f.open(QIODevice::WriteOnly))
                        return ;
                    QDataStream ds(&f);
                    ds.setVersion(QDataStream::Qt_4_5);
                    ds << x;
            
             // load 
                    QFile f(path);
                    if (!f.open(QIODevice::ReadOnly))
                        return ;
                    QDataStream ds(&f);
                    ds.setVersion(QDataStream::Qt_4_5);
                    ds >> x;
            
                    qDebug() << x.size();
            
            

            Anyway, QVector is fine. especially if it can be used directly by the plotting part
            so you don't need to copy data around.

            N 1 Reply Last reply 12 Nov 2018, 08:08
            4
            • M mrjj
              12 Nov 2018, 07:19

              Hi

              Like this ( test code)
              I would test the speed of both since @mranger90 was so kind to provide the json sample.

                  auto const s = 10000000L;
                  auto const path = QStringLiteral("/path/to/file");
                  
                      QVector<int> x;
                      x.resize(s);
              // save
                      QFile f(path);
                      if (!f.open(QIODevice::WriteOnly))
                          return ;
                      QDataStream ds(&f);
                      ds.setVersion(QDataStream::Qt_4_5);
                      ds << x;
              
               // load 
                      QFile f(path);
                      if (!f.open(QIODevice::ReadOnly))
                          return ;
                      QDataStream ds(&f);
                      ds.setVersion(QDataStream::Qt_4_5);
                      ds >> x;
              
                      qDebug() << x.size();
              
              

              Anyway, QVector is fine. especially if it can be used directly by the plotting part
              so you don't need to copy data around.

              N Offline
              N Offline
              neda
              wrote on 12 Nov 2018, 08:08 last edited by
              #6

              @mrjj
              Thank you so much for your help I really appreciate it.

              1 Reply Last reply
              0

              1/6

              11 Nov 2018, 06:14

              • Login

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