Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved Work with json format

    General and Desktop
    3
    3
    1336
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G
      gen1izh last edited by

      Hello!
      Can you give me some examples for read\write json?
      I can not understand how it work in qt.

      For example

      "[ { "Name" : "Bob", "Phonenumber" : 123 },
         { "Name" : "Alice", "Phonenumber" : 321 } ]"
      

      How create and how read it after?

      Thanks

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        What exactly is your problem ?

        Did you already took a look a the JSON Save Game example ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 3
        • mrjj
          mrjj Lifetime Qt Champion last edited by mrjj

          Hi
          I assumed you know json and seen the docs?
          http://www.json.org/
          So you know we got array and objects ?
          Those map directly to QJsonArray and QJsonObject
          So to use it we construct class and set properties and add to lists.
          Your samle is
          An Array with 2 objects.
          So this is how it works as mini sample.
          Note that for production code, you need more error checking. :)

          #include <QJsonObject>
          #include <QJsonArray>
          #include <QJsonDocument>
          #include <QDebug>
          #include <QFile>
          
          QJsonDocument loadJson(QString fileName) {
            QFile jsonFile(fileName);
            jsonFile.open(QFile::ReadOnly); // error checking!
            return QJsonDocument().fromJson(jsonFile.readAll());
          }
          
          void saveJson(QJsonDocument document, QString fileName) {
            QFile jsonFile(fileName);
            jsonFile.open(QFile::WriteOnly); // error checking!
            jsonFile.write(document.toJson());
          }
          // helper function to create a Person JSON Object
          QJsonObject Person(QString Name, QString Phone ) {
            QJsonObject p;
            p["name"] = Name;
            p["phonenumber"] = Phone;
            return p;
          }
          
          void testCreateJSON() {
            // the first element in a json file must be Object or Array. Called the root.
            QJsonArray root;  // we use array as ur sample has array as "root"
            // now add some object to the list. the << adds to the list. you can also use append.
            root << Person("bob", "12121212") << Person("lisa", "9022020202");
            // now make a document and add the root object to it. Note that this is by copy and hence we do it last!
            QJsonDocument doc(root);
            // save it to file. Change path.
            saveJson(doc, "e:/test.json");
            // show
            qDebug() << doc.toJson();
          }
          
          void testReadJSON() {
            // make a doc
            QJsonDocument doc;
            // read it
            doc = loadJson("e:/test.json");
          
            // now we need to parse it. It can start with Object or array.
            // we know we start with array so we consider it error if object. ( in this case)
            //
            if( ! doc.isArray() ) {
              qDebug() << "File starts with object!" ;
              return;
            }
          
            // get the root array into arrayobj
            QJsonArray rootArray = doc.array();
            // loop over array
            for(const QJsonValue& val : rootArray) {
              // everything in list and properties is QJsonValue and u must converto to right type
              // we have objects in list
              QJsonObject loopObj = val.toObject();
              // it could also be toInt etc for other types than string
              qDebug() << loopObj["name"].toString();
              qDebug() << loopObj["phonenumber"].toString();
            }
          
          }
          
          
          1 Reply Last reply Reply Quote 4
          • First post
            Last post