1

I have a QString of JSON-encoded dictionaries. Is there a simple way to convert them to a list of QHashes? I've looked at this post Best JSON parser for Qt?, but haven't been able to get a valid QHash out (says it's empty).

"[{ 'var' : 'xres', 'name' : 'Image Width', 'type' : 'int', 'min' : 1, 'max' : 4096},{ 'var' : 'yres', 'name' : 'Image Height', 'type' : 'int', 'min' : 1, 'max' : 4096}]"

and I'd like them in something like QList<QHash<QString,QVariant>>.

SOLVED:

QScriptValue sc;
QScriptEngine engine;
sc = engine.evaluate(atts); // In new versions it may need to look like engine.evaluate("(" + QString(result) + ")");

QVariantList attsList;
qScriptValueToSequence(sc, attsList);

foreach (QVariant item, attsList) {
    //std::cout << item.typeName() << std::endl;
    QMap<QString,QVariant> attribute = item.toMap();
    attribute["name"].toString() // etc.
Community
  • 1
  • 1
voodoogiant
  • 2,118
  • 6
  • 29
  • 49
  • please post the code you tried so that people can look at it and hopefully fix it. – Mat Jun 24 '11 at 04:48

1 Answers1

1

If you're using QScriptEngine (or QJson) to do the parsing, this puts the properties into a QMap rather than a QHash.

It's easy enough to convert between the two, but it won't be done automatically.

Dan Milburn
  • 5,600
  • 1
  • 25
  • 18