如何将多个QJsonObject添加到QJsonDocument [英] How I can add more than one QJsonObject to a QJsonDocument

查看:1027
本文介绍了如何将多个QJsonObject添加到QJsonDocument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在QJsonDocument上添加多个QJsonObject. 这可能吗?

I want to add more than one QJsonObject to a QJsonDocument. Is this possible?

它应该看起来像这样:

[
    {
        "objID": "obj1"
        //... Some other parameter
    },
    {
        "objID": "obj2"
        //...Some other parameter
    }
]

我尝试过:

QJsonDocument(obj1).toJson(QJsonDocument::Compact);
QJsonDocument(obj2).toJson(QJsonDocument::Compact);

但是它会生成无效的JSON.

But it produces invalid JSON.

推荐答案

一个JSON文档只有一个根值.在您给出的示例中,该值是一个数组,其中包含两个对象

A JSON document has only one root value. In the example you gave, that value is an array, which contains two objects

要在Qt中获得它,您会说:

To get that in Qt, you'd say:

QJsonArray array;
array << obj1;
array << obj2;
QJsonDocument(array).toJson(QJsonDocument::Compact);

这篇关于如何将多个QJsonObject添加到QJsonDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆