Home c++ qt C++ json entry to file

qt C++ json entry to file

Author

Date

Category

I realized how to read json files, but how to write in them?

I tried it like this:

qfile filejson (": / json / json / first.json");
 Filejson.Open (Qiodevice :: WriteOnly);
 Filejson.Write ("Hello!");
 filejson.close ();

But in the console got it:

Tell me how to write to JSON files using QT C++!

p.s. In this example, I just wrote “Hello!”, And you tell me how exactly JSON write)

qvariantmap dict_;
  DICT_.INSERT ("LEVEL", 3);
  QFile Filejson ("./ first.json");
  Filejson.Open (Qiodevice :: WriteOnly);
  Filejson.Write (QJSondocument (QJSONOBJECT :: FROMVARIANTMAP (DICT _)). Tojson ());
  filejson.close ();

opens successfully, but does not write anything to the file


Answer 1, Authority 100%

In order to record a string in JSON format, you should create an object of class QJSONOBJECT , initialize its data (in my example from the QVARIANTMAP class object, but it is not necessary to do it So), convert to QByTearray using qjsondocument and write this sequence bytes to the file, exactly as you do in your example:

qfile filejson ("./ first.json"); // I write a file in the current directory
 Filejson.Open (Qiodevice :: WriteOnly); // everything is true if there is no file, it will be created
 Qvariantmap testmap; // Test data, maybe anything
 testmap.insert ("MSG", "Hello!");
 testmap.insert ("Level", 5);
 Filejson.Write (QJsondocument (QJSONOBJECT :: FROMVARIANTMAP (TESTMAP)). TOJSON ());
 filejson.close ();

as an option, the tojson function can be called with the QJSondocument :: Compact parameter (if you do not need “person chip”):

filejson.write (qjsondocument (qjsonobject :: fromvariantmap (testmap)). Tojson (QJSondocument :: Compact));

The result of the program (contents of the file FIRST.JSON):

{
  "Level": 5,
  "MSG": "Hello!"
}

or (when using QJSondocument :: Compact ):

{"level": 5, "msg": "hello!"}

As for the resource file, the data is recorded there when compiling the project, “legal” methods of dynamic recording Qt does not provide, yes I do not see in such a hacking of a special meaning, because for this there are existed Beautiful regular file system files.


Answer 2, Authority 17%

As you have already noticed, you received a Qiodevice :: Write error due to the incorrect opening of the file, since you used the incorrect absolute path to your file.
Try using qjsonvalue instead of qvariantmap, as an alternative version of the formation of QJSONOBJECT:

qjsonobject jobj;
  jobj.insert ("msg", qjsonvalue :: fromvariant (qvariant ("Hello")));

To write to a file, you can use the example from my library qserializer :

void writetojsonfile (qjsonobject jobj)
  {
    Qjsondocument jdoc (jobj);
    QFile File ("./ first.json");
    If (File.open (Qiodevice :: WriteOnly))
    {
      File.Write (qstring (jdoc.tojson ()). Tostdstring (). C_STR ());
      file.close ();
    }
  }

QJSONDocument :: TJSON returns QByTearray, it remains only to wrap it in QString and get const char * to Qstring data. However, note that Tujson returns JSON in utf-8 encoding.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions