PDA

View Full Version : writing to previously existing json file using Qt5.3



NS_19
12th June 2014, 13:40
I am using QT 5.3. I have read various materials present online describing how to write json file,but no content describes it systematically and stepwise.

It would be really helpful if someone can explain the stepwise process of writing a json file in simple language since i am new to qt.

In my case i have a json file that already exists "LOM.json" with some content.How do i add new data to this.


{ "LOM": [
{ "LOM ID": 1,
"Source": "Open Internet",
"Content": "Complete Reference Java.pdf",
"Difficulty Level": "Hard",
"Type": "Text",
"Length": "Long",
"Topic-Proficiency":
[
{ "Topic": "Programming", "Proficiency": "E2" },
{ "Topic": "Java", "Proficiency": "E3" }
]
},
{ "LOM ID": 2,
"Source": "Open Internet",
"Content": "www.LatexTutorial.com",
"Difficulty Level": "Medium",
"Type": "WebCourse",
"Length": "Medium",
"Topic-Proficiency":
[
{ "Topic": "Latex", "Proficiency": "E2" }
]
}
]
}


Thanks.

Lesiok
12th June 2014, 14:05
Read in Qt doc about this example (http://qt-project.org/doc/qt-5/qtcore-savegame-example.html).

NS_19
13th June 2014, 07:53
Actually the problem is...here in my example each object has a key-value pair "Topic-Proficiency" whose value is of type array of objects.When i try to insert QJsonObject type object to QJsonArray,it is not allowed since QJsonArray can only have QJsonValue type value. Thats's why i am not able to insert ,multiple QJsonObject to QJsonArray.

Added after 1 56 minutes:

I finally got it done.
Actually the mistake was that while declaring the QJsonObject and QjsonArray,i was declaring them as pointer type that's why it was not allowing to insert qjsonobject to qjsonarray.
As far as writing to already existing json file is concerned,firstly the file is to be opened and content is to be read in qjsonarray or object.Next the changes to be done are appended to the read data(in qjson object or qjsonarray) and finally the new value is inserted to the read document by removing the previous one.
Thanks for the help.