Work with json format



  • 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


  • Lifetime Qt Champion

    Hi,

    What exactly is your problem ?

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


  • Qt Champions 2016

    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();
      }
    
    }
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.