将 json 哈希数组转换为 QT QHashes 列表
我有一个 JSON 编码字典的 QString。有没有一种简单的方法将它们转换为 QHashes 列表?我看过这篇文章 Best JSON parser for Qt?,但还没有能够获得有效的 QHash(说它是空的)。
"[{ 'var' : 'xres', 'name' : 'Image Width', 'type' : 'int', 'min' : 1, 'max' : 4096},{ 'var' : 'yres', 'name' : 'Image Height', 'type' : 'int', 'min' : 1, 'max' : 4096}]"
我希望它们像 QList
那样。
已解决:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 QScriptEngine(或 QJson)进行解析,这会将属性放入 QMap 而不是 QHash 中。
两者之间的转换很容易,但不会自动完成。
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.