QT中的QVariantMap Json解析

发布于 2024-09-24 00:35:21 字数 586 浏览 0 评论 0原文

我使用以下代码进行解析:

QJson::Parser parser;
bool ok;
QVariantMap result=parser.parse (cityReply->readAll(),&ok).toMap();
if (!ok)
{
    qFatal("An error occurred during parsing");
    exit (1);
}

foreach (QVariant city, result.toList())
{
    QVariantMap names = city.toMap();
    qDebug() << "\t-" << names["name"].toString();
}

我的 json 字符串是 [{"id":2,"name":"AAA"},{"id":1,"name":"BBB"}]< /代码>。

我收到以下错误:

“class QVariantMap”没有名为“toList”的成员。

是否可以将 QMap 转换为 QList?

I am using the following code for parsing:

QJson::Parser parser;
bool ok;
QVariantMap result=parser.parse (cityReply->readAll(),&ok).toMap();
if (!ok)
{
    qFatal("An error occurred during parsing");
    exit (1);
}

foreach (QVariant city, result.toList())
{
    QVariantMap names = city.toMap();
    qDebug() << "\t-" << names["name"].toString();
}

My json String is [{"id":2,"name":"AAA"},{"id":1,"name":"BBB"}].

I got the following error:

'class QVariantMap' has no member named 'toList'.

is it possible to convert the QMap to QList?

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

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

发布评论

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

评论(1

花开浅夏 2024-10-01 00:35:21

result 包含一个序列化数组作为 QVariant。您需要在调用 toList() 函数之前提取它。由于数组未在 Json 字符串中命名,因此您可以通过获取映射中的第一个 QVariant 并执行您在问题中编写的操作来访问它。

result contain a serialized array as QVariant. You need to extract it before calling the toList() function. Since array is not named in Json string, you can access it by getting the first QVariant in the map and the doing what you have written in the question.

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