-1

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Finder
  • 8,259
  • 8
  • 39
  • 54
  • Have you some errors? Regarding your Json chain it's an array with 2 objects in it. Format seems to be ok. Please provide the your code in order us to see what your are doing – Patrice Bernassola Sep 14 '10 at 12:27
  • I am using the following code, QJson::Parser parser; bool ok; QVariantMap result = parser.parse (cityReply->readAll(), &ok).toMap(); if (!ok) { qFatal("An error occurred during parsing"); exit (1); } qDebug() << "Name :" << result.value("name").toString(); } the output is : Name : "" Note : if I displays cityReply->readAll() in messagebox then I can view the webservice result (json String). – Finder Sep 14 '10 at 14:26
  • I am using the following code, QJson::Parser parser; bool ok; QVariantMap result=parser.parse (cityReply->readAll(),&ok).toMap(); if (!ok) { qFatal("An error occurred during parsing"); exit (1); } qDebug() << "Name :" << result.value("name").toString(); } the output is : Name : "" Note : if I displays cityReply->readAll() in messagebox then I can view the webservice result (json String). – Finder Sep 15 '10 at 03:52

1 Answers1

2

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");
}
Christophe Eblé
  • 8,071
  • 3
  • 33
  • 32