1

I am trying to parse this JSON from a web service.

  [
      {
        "word": "track",
        "score": 4144
      },
      {
        "word": "trail",
        "score": 2378
      },
      {
        "word": "domestic dog",
        "score": 51
      },
      {
        "word": "dogiron"
      }
  ]

When I log out the response from the API call as a QString as below, it comes out fine but with all quotes escaped, thus not making it a valid JSON:

QString response = (QString) data->readAll();
qDebug() << "Return data: \n" << response;

Examples I have seen so far (e.g. Parse jsonarray?) only parse named arrays which they grab from a QJsonObject by name. Any hints on how to use QJsonArray directly or together with QJsonDocument::fromJson() on the return data?

Dut
  • 39
  • 8
  • 1
    Also, you are seeing quotes escaped in your sample code because `QDebug` is escaping them in its output. It does not mean the escape characters are present in the QString. – Daniel Waechter Apr 04 '18 at 01:28
  • That part I get as quotes in a string are expected to be escaped for proper output. My point in that case is that I can't use `QString` as valid JSON array to iterate through. – Dut Apr 04 '18 at 01:44

1 Answers1

7

QJsonDocument has a member called array() that returns the QJsonArray contained in the document.

For example:

QFile file("main.json");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QByteArray jsonData = file.readAll();
file.close();

QJsonParseError parseError;
QJsonDocument document = QJsonDocument::fromJson(jsonData, &parseError);

if (parseError.error != QJsonParseError::NoError)
{
    qDebug() << "Parse error: " << parseError.errorString();
    return;
}

if (!document.isArray())
{
    qDebug() << "Document does not contain array";
    return;
}

QJsonArray array = document.array();

foreach (const QJsonValue & v, array)
{
    QJsonObject obj = v.toObject();
    qDebug() << obj.value("word").toString();
    QJsonValue score = obj.value("score");
    if (!score.isUndefined())
        qDebug() << score.toInt();
}
p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35
Daniel Waechter
  • 2,574
  • 2
  • 21
  • 21
  • And `isArray()` to check whether the document is an array. – bipll Apr 04 '18 at 01:19
  • I tried `QJsonDocument jsdoc = QJsonDocument::fromJson(response); qDebug() << "size: " << jsdoc.array().size(); // but this returns 'size: 0'.` – Dut Apr 04 '18 at 01:25
  • 2
    Try passing a `QJsonParseError` object to `fromJson` to find out if it is hitting a parsing error, and checking `document.isArray()` before fetching the size. – Daniel Waechter Apr 04 '18 at 01:31
  • There is a parse error. I get '`Parse error: "illegal value"`' – Dut Apr 04 '18 at 15:12
  • Then either the byte array contains invalid JSON to begin with or you are performing some bad transformation on it before parsing. I would inspect it in a debugger. – Daniel Waechter Apr 04 '18 at 15:21
  • Doing `QString response = (QString) data->readAll();` was modifying the JSON because `data` was declared as a pointer. Fixed, now working; thanks a bunch. – Dut Apr 04 '18 at 15:58