QVariantMap Json Parsing in QT

I use the following code for parsing:

QJson::Parser parser;
bool ok;
QVariantMap result=parser.parse (cityReply->readAll(),&ok).toMap();
if (!ok)
{
    qFatal("An error occurred during parsing");
    exit (1);
}

foreach (QVariant city, result.toList())
{
    QVariantMap names = city.toMap();
    qDebug() << "\t-" << names["name"].toString();
}

My string is json [{"id":2,"name":"AAA"},{"id":1,"name":"BBB"}].

I got the following error:

'class QVariantMap' does not have a name named 'toList'.

Is it possible to convert a QMap to a QList?

+3
source share
1 answer

resultcontain a serialized array like QVariant. You need to extract it before calling the function toList(). Since the array is not specified in the Json line, you can access it by getting the first QVarianton the map and completing what you wrote in the question.

+3
source

Source: https://habr.com/ru/post/1764868/


All Articles