c++ - Qt - Writing integer data into JSON -


i using qt (5.5) , want exchange data in json format in client-server application.

so format constant:

{     "ball":     {         "posx": 12,         "posy": 35     } } 

i able define bytearray or string so:

qbytearray data = "{\"ball\":{\"posx\":%s,\"posy\":%s}}"

and write whatever values string.

how do that?

qtjson baked qt 5. easy use, , gets ready pretty easily.

#include <qcoreapplication> #include <qdebug> #include <qjsonobject> #include <qjsondocument>  void savetojson(qjsonobject & json);  int main(int argc, char *argv[]) {     qcoreapplication a(argc, argv);      qjsonobject jsonobject;      savetojson(jsonobject);      qjsondocument jsondoc(jsonobject);      qdebug() << "example of qjsondocument::tojson() >>>";     qdebug() << jsondoc.tojson();     qdebug() << "<<<";      return a.exec(); }  void savetojson(qjsonobject & json) {     qjsonobject ball;     ball["posx"] = 12;     ball["posy"] = 35;      json["ball"] = ball; } 

output

example of qjsondocument::tojson() >>> "{     "ball": {         "posx": 12,         "posy": 35     } } " <<< 

note: qdebug() wraps qstring objects in quotes when printing. rid of that, pass qstring qprintable(). , puts endl in @ end of each line.

for more complex example see official:

json save game example

http://doc.qt.io/qt-5/qtcore-json-savegame-example.html

hope helps.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -