c++ - How to create/read/write JSon files in Qt5 -


qt5 has new json parser , want use it. problem isn't clear functions in layman's terms , how write code it. or reading wrong.

i want know code on creating json file in qt5 , "encapsulates" mean.

example: read json file

/* test.json */ {    "appdesc": {       "description": "somedescription",       "message": "somemessage"    },    "appname": {       "description": "home",       "message": "welcome",       "imp":["awesome","best","good"]    } }   void readjson()    {       qstring val;       qfile file;       file.setfilename("test.json");       file.open(qiodevice::readonly | qiodevice::text);       val = file.readall();       file.close();       qwarning() << val;       qjsondocument d = qjsondocument::fromjson(val.toutf8());       qjsonobject sett2 = d.object();       qjsonvalue value = sett2.value(qstring("appname"));       qwarning() << value;       qjsonobject item = value.toobject();       qwarning() << tr("qjsonobject of description: ") << item;        /* in case of string value value , convert string*/       qwarning() << tr("qjsonobject[appname] of description: ") << item["description"];       qjsonvalue subobj = item["description"];       qwarning() << subobj.tostring();        /* in case of array array , convert string*/       qwarning() << tr("qjsonobject[appname] of value: ") << item["imp"];       qjsonarray test = item["imp"].toarray();       qwarning() << test[1].tostring();    } 

output

qjsonvalue(object, qjsonobject({"description": "home","imp": ["awesome","best","good"],"message": "youtube"}) )  "qjsonobject of description: " qjsonobject({"description": "home","imp": ["awesome","best","good"],"message": "youtube"})  "qjsonobject[appname] of description: " qjsonvalue(string, "home")  "home"  "qjsonobject[appname] of value: " qjsonvalue(array, qjsonarray(["awesome","best","good"]) )  "best"  

example: read json string

assign json string below , use readjson() function shown before:

val =    '  {        "appdesc": {           "description": "somedescription",           "message": "somemessage"        },        "appname": {           "description": "home",           "message": "welcome",           "imp":["awesome","best","good"]        }     }'; 

output

qjsonvalue(object, qjsonobject({"description": "home","imp": ["awesome","best","good"],"message": "youtube"}) )  "qjsonobject of description: " qjsonobject({"description": "home","imp": ["awesome","best","good"],"message": "youtube"})  "qjsonobject[appname] of description: " qjsonvalue(string, "home")  "home"  "qjsonobject[appname] of value: " qjsonvalue(array, qjsonarray(["awesome","best","good"]) )  "best"  

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -