使用 QJSON 解析 JSON

发布于 2024-09-19 02:33:14 字数 125 浏览 1 评论 0原文

我的 json 输出看起来像 [{"id":2,"name":"AAA"},{"id":1,"name":"BBB"}]。我想在 mac 中使用 Qjson 解析它。我正在尝试解析但无法获得任何输出。请帮我。

提前致谢。

My json output look like [{"id":2,"name":"AAA"},{"id":1,"name":"BBB"}]. I want to parse this using Qjson in mac. I am trying to parsing but I could not get any outputs . please help me.

Thanks in advance.

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

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

发布评论

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

评论(1

灵芸 2024-09-26 02:33:14

我认为问题很简单,JSON 表示意味着一个记录数组。
您尝试在不使用迭代器或循环的情况下访问记录。

使用下面的代码片段将 QVariant 转换为列表

QVariantList result = parser.parse(cityReply->readAll(), &ok).toList();

,然后循环记录,例如:

foreach(QVariant record, result) {
    QVariantMap map = record.toMap();
    qDebug() << map.value("name");
}

I think that the issue is quite simple, the JSON representation implies an Array of records.
You are trying to access the record without using an iterator or a loop.

Use the snippet below to transform the QVariant into a list

QVariantList result = parser.parse(cityReply->readAll(), &ok).toList();

And then loop against records for example:

foreach(QVariant record, result) {
    QVariantMap map = record.toMap();
    qDebug() << map.value("name");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文