From JSON string to JSON object
Solved
General and Desktop
-
I want to convert JSON strings into JSON objects which I can use in C++. I know of QJsonValue and QJsonObject, I'm probably just using them wrong.
As an example I am passing a const char* which points to:
[{"failure":"onFileError", "file":["~/XMLMPAM/config/simon.txt", "wt"], "op":"open", "success":"onFileWrite"}, {"binary":[10,11,12 ], "op":"write", "text":"123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n"}, {"name":"simon", "op":"save"}, {"binary":[10,11,12 ], "op":"write", "text":"123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n"}, {"name":"simon", "op":"restore"}, {"binary":[25,27,28 ], "op":"write", "text":"HELLO"}, {"op":"close"}]
The above is an array of objects. I start of by creating an instance of QJsonValue:
QJsonValue valJSON(cpszJSON);
I then test for it being an array or an object:
if ( valJSON.isArray() == true || valJSON.isObject() == true ) { objJSON = valJSON.toObject(); }
It doesn't pass either of these tests, I've validated that the JSON string I'm passing is of the correct syntax, why does it fail and is there a way to do this?
I used http://json.parser.online.fr to verify the syntax.
-
See ctor of QJsonValue(const char*) - it creates a new JSon value with the given string. There is nothing about parsing.
You're looking for QJsonDocument::fromJson()