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.
-
@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. -
Hi,
Do you mean the QJsonDocument::array method ?
As the documentation says, it returns the array contained in the document.
-
@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. -
You forgot
#include <QJsonArray>
in the file where you use that method.