Initialising QJsonObject with JSON
-
@KroMignon , thank you, I'm building it up now with:
QJsonObject objCmd, objJSON; QJsonArray aryCmds; objCmd.insert( "check", "a"); objCmd.insert( "command", "read"); objCmd.insert( "length", 1); objCmd.insert( "mode", "binary"); objCmd.insert( "type", "char"); aryCmds.append(objCmd); objCmd.insert("byteorder", "0,1"); objCmd.insert( "check", "32767"); objCmd.insert( "command", "read"); objCmd.insert( "length", 2); objCmd.insert( "mode", "binary"); objCmd.insert( "type", "short"); aryCmds.append(objCmd); objJSON.insert("commands", aryCmds);Still a way to go...
@SPlatten
Your last reply has crossed with my response. You definitely do not want to start breaking your list into hundreds of separateinsert()statements, IMHO. Suggest you look again. Either do it as shown in the stackoverflow post (the accepted solution one), or stick to a single string passed toQJsonDocument::fromJson()(the other answer there, which is @KroMignon's way, and perhaps the nicest/least typing). -
@KroMignon , thank you, I'm building it up now with:
QJsonObject objCmd, objJSON; QJsonArray aryCmds; objCmd.insert( "check", "a"); objCmd.insert( "command", "read"); objCmd.insert( "length", 1); objCmd.insert( "mode", "binary"); objCmd.insert( "type", "char"); aryCmds.append(objCmd); objCmd.insert("byteorder", "0,1"); objCmd.insert( "check", "32767"); objCmd.insert( "command", "read"); objCmd.insert( "length", 2); objCmd.insert( "mode", "binary"); objCmd.insert( "type", "short"); aryCmds.append(objCmd); objJSON.insert("commands", aryCmds);Still a way to go...
@SPlatten said in Initialising QJsonObject with JSON:
Still a way to go...
Yes, I apologize, I have not right understand your question and replied too fast.
The problem is that theQJsonObjectreclams astd::initializer_list<QPair<QString, QJsonValue>()as parameter.
But youcommandis not matching with this type.The easiest way is to use
QJsonDocument::fromJson(), this could be done from a file or from a string. -
@SPlatten said in Initialising QJsonObject with JSON:
Still a way to go...
Yes, I apologize, I have not right understand your question and replied too fast.
The problem is that theQJsonObjectreclams astd::initializer_list<QPair<QString, QJsonValue>()as parameter.
But youcommandis not matching with this type.The easiest way is to use
QJsonDocument::fromJson(), this could be done from a file or from a string.@KroMignon , this is what I have now:
QString strJSON = "{\"commands\":[{\"check\":\"a\"" ",\"command\":\"read\"" ",\"mode\":\"binary\"" ",\"length\":1" ",\"type\":\"char\"}" ",{\"byteorder\":\"0,1\"" ",\"check\":32767" ",\"command\":\"read\"" ",\"length\":2" ",\"mode\":\"binary\"" ",\"type\":\"short\"}" ",{\"byteorder\":\"0,1,2,3\"" ",\"check\":2147483647" ",\"command\":\"read\"" ",\"length\":4" ",\"mode\":\"binary\"" ",\"type\":\"int\"}" ",{\"byteorder\":\"0,1,2,3,4,5,6,7\"" ",\"check\":9223372036854775807" ",\"command\":\"read\"" ",\"length\":8" ",\"mode\":\"binary\"" ",\"type\":\"long\"}" ",{\"byteorder\":\"0,1,2,3\"" ",\"check\":3.4028234663852886e+38" ",\"command\":\"read\"" ",\"length\":4" ",\"mode\":\"binary\"" ",\"type\":\"float\"}" ",{\"byteorder\":\"0,1,2,3,4,5,6,7\"" ",\"check\":1.7976931348623157e+308" ",\"command\":\"read\"" ",\"length\":8" ",\"mode\":\"binary\"" ",\"type\":\"double\"}" ",{\"check\":\"abcdefghijklmnopqrstuvwxyz\"" ",\"command\":\"read\"" ",\"length\":27" ",\"mode\":\"binary\"" ",\"type\":\"string\"}" ",{\"check\":\"b\"" ",\"command\":\"read\"" ",\"length\":1" ",\"mode\":\"binary\"" ",\"type\":\"uchar\"}" ",{\"byteorder\":\"0,1\"" ",\"check\":65535" ",\"command\":\"read\"" ",\"length\":2" ",\"mode\":\"binary\"" ",\"type\":\"ushort\"}" ",{\"byteorder\":\"0,1,2,3\"" ",\"check\":4294967295" ",\"command\":\"read\"" ",\"length\":4" ",\"mode\":\"binary\"" ",\"type\":\"uint\"}" ",{\"byteorder\":\"0,1,2,3,4,5,6,7\"" ",\"check\":18446744073709551615" ",\"command\":\"read\"" ",\"length\":8" ",\"mode\":\"binary\"" ",\"type\":\"ulong\"}" ",{\"command\":\"find\"" ",\"from\":\"start\"" ",\"pattern\":\"JKL\"" ",\"skipover\":false}" ",{\"command\":\"read\"" ",\"length\":3" ",\"mode\":\"text\"}" ",{\"command\":\"read\"" ",\"length\":4" ",\"mode\":\"text\"}" ",{\"command\":\"read\"" ",\"length\":5" ",\"mode\":\"text\"" ",\"position\":13}" ",{\"command\":\"tell\"}]" ",\"file\":\"~/XMLMPAM/config/test.dat\"" ",\"module\":\"mdFileIO\"};"; QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toLatin1());This is all just to test functionality in my application.
-
@KroMignon , this is what I have now:
QString strJSON = "{\"commands\":[{\"check\":\"a\"" ",\"command\":\"read\"" ",\"mode\":\"binary\"" ",\"length\":1" ",\"type\":\"char\"}" ",{\"byteorder\":\"0,1\"" ",\"check\":32767" ",\"command\":\"read\"" ",\"length\":2" ",\"mode\":\"binary\"" ",\"type\":\"short\"}" ",{\"byteorder\":\"0,1,2,3\"" ",\"check\":2147483647" ",\"command\":\"read\"" ",\"length\":4" ",\"mode\":\"binary\"" ",\"type\":\"int\"}" ",{\"byteorder\":\"0,1,2,3,4,5,6,7\"" ",\"check\":9223372036854775807" ",\"command\":\"read\"" ",\"length\":8" ",\"mode\":\"binary\"" ",\"type\":\"long\"}" ",{\"byteorder\":\"0,1,2,3\"" ",\"check\":3.4028234663852886e+38" ",\"command\":\"read\"" ",\"length\":4" ",\"mode\":\"binary\"" ",\"type\":\"float\"}" ",{\"byteorder\":\"0,1,2,3,4,5,6,7\"" ",\"check\":1.7976931348623157e+308" ",\"command\":\"read\"" ",\"length\":8" ",\"mode\":\"binary\"" ",\"type\":\"double\"}" ",{\"check\":\"abcdefghijklmnopqrstuvwxyz\"" ",\"command\":\"read\"" ",\"length\":27" ",\"mode\":\"binary\"" ",\"type\":\"string\"}" ",{\"check\":\"b\"" ",\"command\":\"read\"" ",\"length\":1" ",\"mode\":\"binary\"" ",\"type\":\"uchar\"}" ",{\"byteorder\":\"0,1\"" ",\"check\":65535" ",\"command\":\"read\"" ",\"length\":2" ",\"mode\":\"binary\"" ",\"type\":\"ushort\"}" ",{\"byteorder\":\"0,1,2,3\"" ",\"check\":4294967295" ",\"command\":\"read\"" ",\"length\":4" ",\"mode\":\"binary\"" ",\"type\":\"uint\"}" ",{\"byteorder\":\"0,1,2,3,4,5,6,7\"" ",\"check\":18446744073709551615" ",\"command\":\"read\"" ",\"length\":8" ",\"mode\":\"binary\"" ",\"type\":\"ulong\"}" ",{\"command\":\"find\"" ",\"from\":\"start\"" ",\"pattern\":\"JKL\"" ",\"skipover\":false}" ",{\"command\":\"read\"" ",\"length\":3" ",\"mode\":\"text\"}" ",{\"command\":\"read\"" ",\"length\":4" ",\"mode\":\"text\"}" ",{\"command\":\"read\"" ",\"length\":5" ",\"mode\":\"text\"" ",\"position\":13}" ",{\"command\":\"tell\"}]" ",\"file\":\"~/XMLMPAM/config/test.dat\"" ",\"module\":\"mdFileIO\"};"; QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toLatin1());This is all just to test functionality in my application.
@SPlatten said in Initialising QJsonObject with JSON:
QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toLatin1());
I would change this to
QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toUtf8());
Because, as written in documentation, and UTF-8 encoded JSON document is awaited:[static]QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = nullptr)
Parses json as a UTF-8 encoded JSON document, and creates a QJsonDocument from it.
But I do not understand, do it work for you or do you still have trouble?
-
@SPlatten said in Initialising QJsonObject with JSON:
QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toLatin1());
I would change this to
QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toUtf8());
Because, as written in documentation, and UTF-8 encoded JSON document is awaited:[static]QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = nullptr)
Parses json as a UTF-8 encoded JSON document, and creates a QJsonDocument from it.
But I do not understand, do it work for you or do you still have trouble?
@KroMignon , I need to produce a QJsonObject from the QJsonDocument, so far this isn't working, the JSON is valid, but:
QJsonObject objJSON = objJSON.object();Returns and empty object.
-
@KroMignon , I need to produce a QJsonObject from the QJsonDocument, so far this isn't working, the JSON is valid, but:
QJsonObject objJSON = objJSON.object();Returns and empty object.
-
QJsonObject objJSON = objJSON.object();
How can this possibly be right? You have the same variable name,
objJSON, on both sides of the=!!For your literal, where you have embedded
\"s everywhere, you should use theR"(C++ literal syntax. -
@JonB , sorry that was a typo, the actual code is:
QJsonObject objJSON = docJSON.object();As I said, objJSON contains (0 items).
-
@SPlatten
In the past I told you to check the return result fromQJsonDocument::fromJson(), and pass in the optionalQJsonParseError *errorparameter to it....@JonB said in Initialising QJsonObject with JSON:
al QJsonParseError *error
QJsonParseError error; QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toUtf8(), &error); QJsonObject objJSON = docJSON.object();error is 14, offset 1410, 14 =:
The parsed document contains additional garbage characters at the endLooks like I don't need the ';' at the end.
[edit], yes that was it. thank you. -
@JonB said in Initialising QJsonObject with JSON:
al QJsonParseError *error
QJsonParseError error; QJsonDocument docJSON = QJsonDocument::fromJson(strJSON.toUtf8(), &error); QJsonObject objJSON = docJSON.object();error is 14, offset 1410, 14 =:
The parsed document contains additional garbage characters at the endLooks like I don't need the ';' at the end.
[edit], yes that was it. thank you.@SPlatten
Please, I did suggest to you, start by using theR"(C++ literal syntax. I don't know where in your string there is an error, you'll have to check the offset....Looks like I don't need the ';' at the end.
Yep, the JSON does not want/parse any
;....Now you will test the returned
QJsonDocument::fromJson()forisNull(), and pass in the error return variable from all of your calls from now onward won't you? :)