Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Work with json format
Forum Updated to NodeBB v4.3 + New Features

Work with json format

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.6k Views 2 Watching
  • 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 Offline
    G Offline
    gen1izh
    wrote on 31 Dec 2016, 15:30 last edited by
    #1

    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
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 31 Dec 2016, 22:26 last edited by
      #2

      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
      3
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 1 Jan 2017, 02:52 last edited by mrjj 1 Jan 2017, 02:54
        #3

        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
        4

        1/3

        31 Dec 2016, 15:30

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved