I want store my datas in JSON file like:
{
    "plottingData": [
        {
            "min": 17,
            "max": 35,
            "mean": 20
        },
        {
            "min": 7,
            "max": 35,
            "mean": 17
        },
        {
            "min": 8,
            "max": 50,
            "mean": 29
        }
    ]
}
How can I create this struct? I used to QJsonObject but I couldn't add QJsonArray like this.
Array Datatype in JSON Similar to other programming languages, a JSON Array is a list of items surrounded in square brackets ([]). Each item in the array is separated by a comma. The array index begins with 0. The square brackets [...] are used to declare JSON array.
A JSON array contains zero, one, or more ordered elements, separated by a comma. The JSON array is surrounded by square brackets [ ] . A JSON array is zero terminated, the first index of the array is zero (0). Therefore, the last index of the array is length - 1.
Qt provides support for dealing with JSON data. JSON is a format to encode object data derived from Javascript, but now widely used as a data exchange format on the internet. The JSON support in Qt provides an easy to use C++ API to parse, modify and save JSON data.
From Qt documentation:
QJsonArray plot_array;
// use initializer list to construct QJsonObject
auto data1 = QJsonObject(
{
qMakePair(QString("min"), QJsonValue(17)),
qMakePair(QString("max"), QJsonValue(35)),
qMakePair(QString("mean"), QJsonValue(20))
});
plot_array.push_back(QJsonValue(data1));
// Or use insert method to create your QJsonObject
QString min_str("min");
QString max_str("max");
QString mean_str("mean");
for(auto item : your_collection)
{
    QJsonObject item_data;
    item_data.insert(min_str, QJsonValue(item.min));
    item_data.insert(max_str, QJsonValue(item.max));
    item_data.insert(mean_str, QJsonValue(item.mean));
    plot_array.push_back(QJsonValue(item_data));
}
QJsonObject final_object;
final_object.insert(QString("plottingData"), QJsonValue(plot_array));
                         QJsonObject o1
 {
     { "min", 17 },
     { "max", 35 },
     { "mean", 20 },
 };
 QJsonObject o2;
 o2 [ "min" ] = 7;
 o2 [ "max" ] = 35;
 o2 [ "mean"] = 17;
 QJsonArray arr;
 arr.append ( o1 );
 arr.append ( o2 );
 QJsonObject obj;
 obj [ "plottingData" ] = arr;
 //to see the JSON output
 QJsonDocument doc ( obj );
 qDebug() << doc.toJson ( QJsonDocument::Compact );  // or QJsonDocument::Indented
 // output: "{\"plottingData\":[{\"max\":35,\"mean\":20,\"min\":17},{\"max\":35,\"mean\":17,\"min\":7}]}"
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With