02-05-2013 06:19 AM - edited 02-05-2013 06:21 AM
Hi,
I have some JSON data which I import into a QVariantList, and using code below
I am trying to traverse this list and print out the items. But I have two issues:
JsonDataAccess jda;
QString path;
path = QDir::currentPath() +
"/app/native/assets/employees.json";
QVariant list = jda.load(path);
QVariantList temp = list.value<QVariantList>();
for (int i = 0; i < temp.size(); ++i) {
QString gStr = temp[i].toString();
qDebug()<<"String Value: "<<gStr;
}1. How to use qDebug properly in Momentics IDE? OS: Mac OS X; nothing gets printed even if I remove the gStr variable from that line
2. Is it correct way to traverse the QVariantList and print contents of its elements?
ps. the temp is correctly initialized I manage to fill a ListView using it later on.
Solved! Go to Solution.
02-05-2013 06:30 AM
Hi,
1. Please check "Known limitations" section in SDK release notes, it contains information on setting up the logs:
https://developer.blackberry.com/cascades/download
2. The code is correct. Another way to iterate the list:
foreach (QVariant v, temp)
{
qDebug() << "String Value: " << v.toString() << "\n";
}
You might also want to add newline to flush the output otherwise last line can be lost on application crash.
Also foreach makes a copy of list before iterating. Qt performs lazy copying of data (copy-on-write), so this isn't very expensive and you can safely modify the container during iteration.
02-05-2013 06:51 AM - edited 02-05-2013 06:53 AM
Yep, thanks. just one brief question. In my case it appears my "temp" is an array of 3 objects. Where clearly each object is a QVariantMap. Now I want to print out the contents of some keys (say "key1") in these QVariantMaps objects. do you have some code snippet that would do this? ps. I actually have issues converting the QVariantList item to a QVariantMap; I know how to print contents of a QVariantMap
02-05-2013 06:58 AM
QVariantMap m = v.value<QVariantMap>(); qDebug() << m["key"].toString();
02-05-2013 07:05 AM - edited 02-05-2013 07:07 AM
Yes I was about to write I had found a similar solution; but now my debug logging is not working.. a anyway thanks I will try to find smth.