Search code examples
qtqt4qjson

Parsing JSON using QJSON


My json output look like [{"id":2,"name":"AAA"},{"id":1,"name":"BBB"}]. I want to parse this using Qjson in mac. I am trying to parsing but I could not get any outputs . please help me.

Thanks in advance.


Solution

  • I think that the issue is quite simple, the JSON representation implies an Array of records. You are trying to access the record without using an iterator or a loop.

    Use the snippet below to transform the QVariant into a list

    QVariantList result = parser.parse(cityReply->readAll(), &ok).toList();
    

    And then loop against records for example:

    foreach(QVariant record, result) {
        QVariantMap map = record.toMap();
        qDebug() << map.value("name");
    }