Results 1 to 5 of 5

Thread: reading JSON file in Qt5

  1. #1
    Join Date
    Jan 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default reading JSON file in Qt5

    Hi,

    I have been trying to figure out how to read a JSON file using Qt5 QJsonDocument with no success so far.
    I can't find any examples online. If someone could give me a simple example I would appreciate it.

    Here is my code::
    Qt Code:
    1. QFile file;
    2. file.setFileName("/tmp/settings.json");
    3. file.open(QIODevice::ReadOnly | QIODevice::Text);
    4. QJsonDocument settdoc;
    5. settdoc.fromBinaryData(file.readAll());
    6. qDebug() << settdoc.isNull();
    To copy to clipboard, switch view to plain text mode 

    settdoc.isNull() returns true, I can read the file into a stream without any issue and I validated the json as well.

    Thank you

    Laszlo
    Last edited by Lykurg; 18th January 2013 at 08:40. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: reading JSON file in Qt5

    Does /tmp/settings.json exists? Is it well formed? Also try QJsonDocument::fromJson and see what error (QJsonParseError) you get.

    EDIT: Forget the first questions....

    EDIT 2:
    Qt Code:
    1. QString str = "{"
    2. " \"Herausgeber\": \"Xema\","
    3. " \"Nummer\": \"1234-5678-9012-3456\","
    4. " \"Deckung\": 2e+6,"
    5. " \"Währung\": \"EURO\","
    6. " \"Inhaber\": {"
    7. " \"Name\": \"Mustermann\","
    8. " \"Vorname\": \"Max\","
    9. " \"männlich\": true,"
    10. " \"Hobbys\": [ \"Reiten\", \"Golfen\", \"Lesen\" ],"
    11. " \"Alter\": 42,"
    12. " \"Kinder\": [],"
    13. " \"Partner\": null"
    14. " }"
    15. "}";
    16. QJsonDocument d = QJsonDocument::fromJson(str.toUtf8());
    17. qWarning() << d.isNull();
    To copy to clipboard, switch view to plain text mode 
    See if that works for you and then replace str.toUtf8() through your readAll().
    Last edited by Lykurg; 18th January 2013 at 08:51.

  3. #3
    Join Date
    Jan 2013
    Posts
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Wink [Solved] Re: reading JSON file in Qt5

    Thank you for your fast reply and even more thank you for your example code!!
    It works now! I somehow overlooked that I can use toUtf8() on a QString.
    You example put me in the right track and now I can adjust basic window properties with my json file
    Now just need to work on auto copy this file into the apps resources at runtime.

    Here is my working code::

    Qt Code:
    1. QString settings;
    2. QFile file;
    3. file.setFileName("/tmp/settings.json");
    4. file.open(QIODevice::ReadOnly | QIODevice::Text);
    5. settings = file.readAll();
    6. file.close();
    7.  
    8. QJsonDocument sd = QJsonDocument::fromJson(settings.toUtf8());
    9. qWarning() << sd.isNull(); // <- print false :)
    10. QJsonObject sett2 = sd.object();
    11. qWarning() << sett2.value(QString("title")); // <- print my title
    To copy to clipboard, switch view to plain text mode 


    Thanks again, totally made my morning

    Laszlo

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [Solved] Re: reading JSON file in Qt5

    You are aware that this requires two codec conversions, right?

    Qt Code:
    1. settings = file.readAll()
    To copy to clipboard, switch view to plain text mode 

    converts from UTF-8 to UTF-16 and

    Qt Code:
    1. settings.toUtf8()
    To copy to clipboard, switch view to plain text mode 

    converts from UTF-16 to UTF-8.

    Since start and end encoding are the same (UTF-8) those two conversions are unneeded.

    Why don't you just call fromJson() with the QByteArray returned by file.readAll()?

    Also: the problem with your original code has nothing to do with the content at all, you are calling a static method and are then wondering why it didn't change an instance.

    Static methods can't do that.
    Hint: in Qt methods that are called fromABCD usually return something.

    Cheers,
    _

  5. #5
    Join Date
    Aug 2017
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: [Solved] Re: reading JSON file in Qt5

    QString filename = QFileDialog::getOpenFileName();

    QString val;
    QFile file;
    file.setFileName(filename);
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    val = file.readAll();
    file.close();
    QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
    QJsonObject Documento = d.object();

    //{}= objet [] Array

Similar Threads

  1. Using external Json file in QML
    By Kelteseth in forum Qt Quick
    Replies: 0
    Last Post: 30th November 2012, 18:08
  2. Reading XML file into a data file correctly?
    By falconium in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2011, 19:55
  3. Reading XML file using DOM
    By rk0747 in forum Qt Programming
    Replies: 6
    Last Post: 4th February 2010, 23:09
  4. help in reading XML file
    By cshiva_in in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2008, 14:55
  5. reading from a file
    By mickey in forum General Programming
    Replies: 32
    Last Post: 19th July 2007, 02:04

Tags for this Thread

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.