Creating json file with subroot
-
How can i create such a json file? I didnt found any information like subroot etc.
{ "Plan" : [ { "route": { "no" : 1.12, "Starting pt" : 1.19, "Total pt" :5 }, "pt info": { "latitude" : 30.42, "longitude" : 41.19, "no" :5 } } ] }
-
What exactly did you try? It's a simply QJsonObject which contains an array of QJsonObjects.
-
@Christian-Ehrlicher
I have written a code like this but it does not work as i wantQJsonArray root; QJsonObject route; QJsonObject route_pt; route.insert(" no", 5); route.insert("starting pt", 7); route.insert("total pt", 3); root.append(route); route_pt.insert("latitude", 5); route_pt.insert("longitude", 3); route_pt.insert("no", 5); root.append(rota_nokta); QJsonDocument doc; doc.setArray(root);
-
You forgot the
Plan
object -
@Christian-Ehrlicher it already gives an error in the last line.
doc.setArray(root);
: no matching function for call to ‘QJsonDocument::setObject(QJsonArray&)’ doc.setObject(root); ^
-
@suslucoder said in Creating json file with subroot:
QJsonDocument::setObject(QJsonArray&)’
This is not what you showed us above!
doc.setArray(root);
-
@Christian-Ehrlicher I mixed my code sorry.
I fixed my problem. Now it seems like: How should i add plan, and route and pt info headings[ { "starting pt": 7, "no": 5, "Total pt": 3 }, { "latitude": 3, "longitude": 5, "no": 5, } ]
-
I don't understand your question. Already told you that the array is in an object.
-
@Christian-Ehrlicher this is what i want to achieve:
{ "Plan" : [ { "route": { "no" : 1.12, "Starting pt" : 1.19, "Total pt" :5 }, "pt info": { "latitude" : 30.42, "longitude" : 41.19, "no" :5 } } ] }
And here is mine:
[ { "starting pt": 7, "no": 5, "Total pt": 3 }, { "latitude": 3, "longitude": 5, "no": 5, } ]
They are so different from each other. I didnt understand, how can i set a root name like plan, or route and pt info. This is my question
-
@suslucoder said in Creating json file with subroot:
This is my question
It's a simply QJsonObject which contains an array
-
@Christian-Ehrlicher said in Creating json file with subroot:
It's a simply QJsonObject which contains an array
This sentence helped me a lot (!)
-
@suslucoder said in Creating json file with subroot:
This sentence helped me a lot (!)
What is so hard to understand?
QJsonObject plan; plan.insert("Plan", root); QJsonDocument doc(plan); qDebug() << QString::fromLatin1(doc.toJson());
EDIT: it would be better to rename your QJsonArray object
root
toplanArray
andplan
toroot
to better match with your json document structure -
@KroMignon thank you