Results 1 to 2 of 2

Thread: Data gets overwritten in Json file

  1. #1
    Join Date
    Mar 2016
    Posts
    5
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Question Data gets overwritten in Json file

    Hi,

    I'm not able to append mutliple data to Json file. My code is as;

    Qt Code:
    1. void MainWindow::fileWriteOperationJson()
    2. {
    3. QString filename = "C:/Users/..../save.json";
    4.  
    5. QFile saveFile(filename);
    6. saveFile.open(QIODevice::WriteOnly|QIODevice::Text);
    7.  
    8. // if (!saveFile.open(QIODevice::WriteOnly))
    9. // {
    10. // qWarning("Couldn't open save file.");
    11. // }
    12. QJsonObject json;
    13. write(json);
    14. QTextStream out(&saveFile);
    15.  
    16. out << QJsonDocument(json).toJson(QJsonDocument::Indented);
    17.  
    18. }
    19.  
    20.  
    21.  
    22. void MainWindow::write(QJsonObject &json) const
    23. {
    24. QJsonObject nameObj;
    25. nameObj["f_name"] = ui->lineEdit->text();
    26. nameObj["l_name"] = ui->lineEdit_2->text();
    27.  
    28. QJsonArray personalData;
    29. personalData.append(nameObj);
    30. json["personal_Data"] = personalData;
    31.  
    32. qDebug() << qlist;
    33. }
    To copy to clipboard, switch view to plain text mode 

    My current output is as:
    Qt Code:
    1. {
    2. "personal_Data": [
    3. {
    4. "f_name": "qqq",
    5. "l_name": "www"
    6. }
    7. ]
    8. }
    To copy to clipboard, switch view to plain text mode 

    Please suggest how should i append the multiple info??

    Thanks in advance

    -Shoeb
    Last edited by anda_skoa; 5th April 2016 at 12:20. Reason: missing [code] tags

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Data gets overwritten in Json file

    if you want a list of JSON objects representing people then you should use a QJsonArray (list) of QJSonObject (people).

Similar Threads

  1. not able to save form data to json file
    By shoeb in forum Newbie
    Replies: 4
    Last Post: 1st April 2016, 11:23
  2. how to login to the application using JSON data in Qt
    By Radhika in forum Qt Programming
    Replies: 4
    Last Post: 7th December 2015, 08:25
  3. Correct POST html with JSON data
    By Mbded in forum Qt Programming
    Replies: 11
    Last Post: 23rd August 2015, 10:19
  4. How to load base64 image data from json in QT
    By phpkode in forum Qt Programming
    Replies: 4
    Last Post: 9th January 2015, 11:10
  5. model/view json data displaying
    By binaural in forum Qt Programming
    Replies: 5
    Last Post: 27th June 2012, 08:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.