Create JSON-File
-
Hey,
iam trying to create in QT5 a JSON-File that Looks like:
{ "ConfigFile": [ { "name": "Car", "valueName": "CarValue", "actual": { "actual": 140 }, "upper": { "actual": 120.1 }, "lower": { "actual": 2 } }, { "name": "Car2", "valueName": "CarValue2", "actual": { "actual": 1.2 }, "upper": { "actual": 22.1 }, "lower": { "actual": 5 } } ] }
I try to created with QJsonArray , QJsonObject and QJsonDocument.
-
@NotYourFan What's your question?
-
how can i create a JSON-File with this structure ?
-
@NotYourFan Using the classes you mentioned.
What exactly is the problem? -
i dont know how to get this structure with QT.
I want to get later a file with exactly the structure like at the top. -
@NotYourFan Take a look at this example: https://doc.qt.io/qt-5/qtcore-serialization-savegame-example.html
-
okay, so now i cant insert into a JsonObject:
QJsonObject object; object.insert("name", hgvc->value)
how can i insert a String and a double into a Object??
-
@NotYourFan Hi,
Just use the same way to insert string or double valuesQJsonObject json; json["string"] = "Hello World"; json["double"] = 1.234 json["int"] = 1234 QJsonArray a; a.append(1); a.append(2); a.append(3); a.append(4); json["array"] = a; QJsonObject child; child["string"] = "Hello World"; child["double"] = 1.234; child["int"] = 1234; json["child_object"] = child; //Retrieve values QString str = json["string"].toString(); double d = json["double"].toDouble(); int i = json["int"].toInt(); QJsonArray a = json["array"].toArray(); for(int ai = 0; ai < a.size(); ai++){ int value = a.at(i).toInt(); } QJsonObject child = json["child_object"].toObject(); QString strChild = child["string"].toString(); double dChild = child["double"].toDouble(); int iChild = child["int"].toInt();
-
Hey @Gojir4 thank you for your answer :)
i have a Problem with the Depth.
{ "ConfigFile": [ { "name": "Car", "valueName": "CarValue", "actual": { "actual": 140 }, "upper": { "actual": 120.1 }, "lower": { "actual": 2 } }, { "name": "Car2", "valueName": "CarValue2", "actual": { "actual": 1.2 }, "upper": { "actual": 22.1 }, "lower": { "actual": 5 } } ] }
do you have a explcite exmaple?
Thank You !!