Load JSON file
-
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.
Thanksfile:
{ "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)); }
-
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 ? -
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(); }
-
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 ?@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?
-
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. -
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.