3

I am using 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 json String is [{"id":2,"name":"AAA"},{"id":1,"name":"BBB"}].

I got the following error:

'class QVariantMap' has no member named 'toList'.

is it possible to convert the QMap to QList?

user3355317
  • 35
  • 1
  • 6
Finder
  • 8,259
  • 8
  • 39
  • 54

1 Answers1

3

result contain a serialized array as QVariant. You need to extract it before calling the toList() function. Since array is not named in Json string, you can access it by getting the first QVariant in the map and the doing what you have written in the question.

Patrice Bernassola
  • 14,136
  • 6
  • 46
  • 59
  • sorry.. could you please explain little bit more. – Finder Sep 16 '10 at 06:14
  • 2
    Regarding the Json string, the QVariantMap only contains one element: the array. Just get it using the QMap function and then convert it to a list – Patrice Bernassola Sep 16 '10 at 07:50
  • @PatriceBernassola Can you please post a code sample using QMap to extract from the array as suggested in the answer? I am new to this api and have a similar problem. My code resembles the above with the exception that my json contains an array name too i.e. [{"chan":{"id":1, "name":"one"}},{"chan":{"id":2,"name":"two"}}] – fkl Jan 01 '13 at 08:02
  • QJson official documentation provide code example for each type of serialized data: http://qjson.sourceforge.net/usage/. Check how plugins array is deserialized. – Patrice Bernassola Jan 01 '13 at 09:40