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. Writing a json file!
Forum Updated to NodeBB v4.3 + New Features

Writing a json file!

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 560 Views 1 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.
  • K Offline
    K Offline
    Kris Revi
    wrote on last edited by
    #1

    What i need it to look like

    {
        "Device": [
            {
                "IPA": "studiolights",
                "Icon": "fa_bolt",
                "Name": "Studio Lights",
                "Status": "online",
                "Type": "Strip",
                "UVLights": false
            }
        ]
    }
    

    what i have so far

                QJsonObject jsonObject;
                jsonObject.insert("IPA", "studiolights");
                jsonObject.insert("Icon", "fa_bolt");
                jsonObject.insert("Name", "Studio Lights");
                jsonObject.insert("Status", "online");
                jsonObject.insert("Type", "Strip");
                jsonObject.insert("UVLights", "false");
    
                QJsonDocument jsonDoc;
                jsonDoc.setObject(jsonObject);
    
                file.write(jsonDoc.toJson());
    
                file.close();
    

    how to i insert the "Device": [ ? :S

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Create a QJsonArray to contain your list of objects and then an object to contain that array.

      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
      2
      • K Kris Revi

        What i need it to look like

        {
            "Device": [
                {
                    "IPA": "studiolights",
                    "Icon": "fa_bolt",
                    "Name": "Studio Lights",
                    "Status": "online",
                    "Type": "Strip",
                    "UVLights": false
                }
            ]
        }
        

        what i have so far

                    QJsonObject jsonObject;
                    jsonObject.insert("IPA", "studiolights");
                    jsonObject.insert("Icon", "fa_bolt");
                    jsonObject.insert("Name", "Studio Lights");
                    jsonObject.insert("Status", "online");
                    jsonObject.insert("Type", "Strip");
                    jsonObject.insert("UVLights", "false");
        
                    QJsonDocument jsonDoc;
                    jsonDoc.setObject(jsonObject);
        
                    file.write(jsonDoc.toJson());
        
                    file.close();
        

        how to i insert the "Device": [ ? :S

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @Kris-Revi
        From the [ ... ] I think you have a QJsonObject with name Device and its value is a QJsonValue which is a QJsonArray. Insert that between your QJsonDocument and your QJsonObject jsonObject.

        K 1 Reply Last reply
        2
        • JonBJ JonB

          @Kris-Revi
          From the [ ... ] I think you have a QJsonObject with name Device and its value is a QJsonValue which is a QJsonArray. Insert that between your QJsonDocument and your QJsonObject jsonObject.

          K Offline
          K Offline
          Kris Revi
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kris Revi
            wrote on last edited by
            #5

            got it working :) thanks @JonB and @SGaist

                        QJsonObject studiolights;
            
                        studiolights.insert("IPA", "studiolights");
                        studiolights.insert("Icon", "fa_bolt");
                        studiolights.insert("Name", "Studio Lights");
                        studiolights.insert("Status", "online");
                        studiolights.insert("Type", "Strip");
                        studiolights.insert("UVLights", "false");
            
                        QJsonArray devices;
            
                        devices.append(studiolights);
            
                        QJsonObject obj;
                        obj["Device"] = devices;
            
                        QJsonDocument jsonDoc;
                        jsonDoc.setObject(obj);
            
                        file.write(jsonDoc.toJson());
            
                        file.close();
            
            1 Reply Last reply
            3

            • Login

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