Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QJsonDocument, array method to create QJsonArray?
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.
JonB last edited by JonB
If your JSON source has arrays in it, your
QJsonDocument::fromJson()-generated document should have
QJsonArrays in it? And you walk/find them like any other JSON element.
QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = nullptr)to pass in a
&errorto get clues about syntax errors.
stance of QJsonArray, but there are syntax errors.
Is there any good example on how to extract the array from the document?
Thank you, I see that there are functions isArray and toArray, what is the array function, I cannot see how to use that?
Do you mean the QJsonDocument::array method ?
As the documentation says, it returns the array contained in the document.
Yes, I get that but you cannot assign the array the function returns to a QJsonArray.
JonB last edited by
I don't understand. It returns the
QJsonArrayif the (top-level of) the
QJsonArray. Why do you want to assign that to a
QJsonArray? If you really want to, there are things like
QJsonArray::QJsonArray(const QJsonArray &other)or
QJsonDocument::setArray(const QJsonArray &array), but not sure what you are trying to achieve.
Try it yourself, I have, you can't there is a problem:
calling 'array' with incomplete return type 'QJsonArray' variable has incomplete type 'QJsonArray'.
in the file where you use that method.