Initialising QJsonObject with JSON
-
I have the following JSON:
{"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" ,"command":"read" ,"mode":"binary" ,"length":4 ,"type":"int" ,"check":2147483647} ,{"byteorder":"0,1,2,3,4,5,6,7" ,"check":9223372036854775807 ,"command":"read" ,"length":8 ,"mode":"binary" ,"type":"long"} ,{"byteorder":"0,1,2,3,4,5,6,7" ,"check":3.4028234663852886e+38 ,"command":"read" ,"length":8 ,"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"}
I've used an online validation tool:
https://jsonlint.com
The above is valid JSON. How do I initialise an instance of QJsonObject with this? Currently I get:
expected '}'
to the right of the first line when I try:
QJsonObject objJSON {"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" ,"command":"read" ,"mode":"binary" ,"length":4 ,"type":"int" ,"check":2147483647} ,{"byteorder":"0,1,2,3,4,5,6,7" ,"check":9223372036854775807 ,"command":"read" ,"length":8 ,"mode":"binary" ,"type":"long"} ,{"byteorder":"0,1,2,3,4,5,6,7" ,"check":3.4028234663852886e+38 ,"command":"read" ,"length":8 ,"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"};
-
@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:
QJsonObject
Parsing JSON should be done with
QJsonDocument
notQJsonObject
:QString jsonString; // load with JSON string QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString); QJsonObject jsonObject = jsonDoc.object();
Edit: sorry, bad reply, did not read correctly your request!
Never usedQJsonObject
initialization withstd::initializer_list()
, I suppose nestedstd::initializer_list
is not supported. -
@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 syntax is wrong because you are mixing JSON syntax ("name" : ...
) with C++ syntax.I don't know that @KroMignon is right, saying you must go via
QJsonObject
.I was about to tell you to look at https://stackoverflow.com/questions/50647381/qt-jsonobjects-and-initializer-lists-deeper-than-one-level, which gives you a couple of alternative syntaxes for initializing
QJsonObject
from a literal. -
@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). -
@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 theQJsonObject
reclams astd::initializer_list<QPair<QString, QJsonValue>()
as parameter.
But youcommand
is 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.
-
@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?
-
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 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 end
Looks 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? :)