QJsonDocument, array method to create QJsonArray?
-
wrote on 12 Apr 2020, 10:29 last edited by
I have created an instance of QJsonDocument:
QJsonDocument docJSON = QJsonDocument::fromJson(cpszJSON);
I can see the docJSON has a method array, how do I extract the array from it and convert to an instance of QJsonArray.
I've tried using it to initialise an instance of QJsonArray, but there are syntax errors.
I want to get the number of elements in the array and pass each element.
-
I have created an instance of QJsonDocument:
QJsonDocument docJSON = QJsonDocument::fromJson(cpszJSON);
I can see the docJSON has a method array, how do I extract the array from it and convert to an instance of QJsonArray.
I've tried using it to initialise an instance of QJsonArray, but there are syntax errors.
I want to get the number of elements in the array and pass each element.
wrote on 12 Apr 2020, 10:35 last edited by JonB 4 Dec 2020, 10:37@SPlatten
If your JSON source has arrays in it, yourQJsonDocument::fromJson()
-generated document should haveQJsonArray
s in it? And you walk/find them like any other JSON element.Use the
QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = nullptr)
to pass in a&error
to get clues about syntax errors. -
wrote on 12 Apr 2020, 10:42 last edited by
@SPlatten said in QJsonDocument, array method to create QJsonArray?:
stance of QJsonArray, but there are syntax errors.
Is there any good example on how to extract the array from the document?
-
wrote on 12 Apr 2020, 11:27 last edited by
Thank you, I see that there are functions isArray and toArray, what is the array function, I cannot see how to use that?
-
Hi,
Do you mean the QJsonDocument::array method ?
As the documentation says, it returns the array contained in the document.
-
wrote on 13 Apr 2020, 06:30 last edited by
Yes, I get that but you cannot assign the array the function returns to a QJsonArray.
-
wrote on 13 Apr 2020, 06:56 last edited by
@SPlatten
I don't understand. It returns theQJsonArray
if the (top-level of) theQJsonDocument
is aQJsonArray
. Why do you want to assign that to aQJsonArray
? If you really want to, there are things likeQJsonArray::QJsonArray(const QJsonArray &other)
orQJsonDocument::setArray(const QJsonArray &array)
, but not sure what you are trying to achieve. -
wrote on 13 Apr 2020, 08:00 last edited by
Try it yourself, I have, you can't there is a problem:
calling 'array' with incomplete return type 'QJsonArray' variable has incomplete type 'QJsonArray'.
-
You forgot
#include <QJsonArray>
in the file where you use that method.
1/10