将 json 哈希数组转换为 QT QHashes 列表

发布于 2024-11-17 02:27:55 字数 943 浏览 6 评论 0 原文

我有一个 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宫墨修音 2024-11-24 02:27:55

如果您使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文