convert json array of hashes to Qt list of QHashes
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 ge开发者_Go百科t 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.
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.
精彩评论