Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create/read/write JSON files in Qt5

Tags:

c++

json

qt

qt5

Qt5 has a new JSON parser and I want to use it. The problem is that it isn't too clear about what the functions do in layman's terms and how to write code with it. That or I could be reading it wrong.

I want to know the code on creating a JSON file in Qt5 and what "encapsulates" mean.

like image 729
Jim Kieger Avatar asked Apr 09 '13 03:04

Jim Kieger


People also ask

Does Qt support JSON?

Qt provides support for dealing with JSON data. JSON is a format to encode object data derived from Javascript, but now widely used as a data exchange format on the internet. The JSON support in Qt provides an easy to use C++ API to parse, modify and save JSON data.

What is JSON format?

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).


1 Answers

Example: Read json from 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 get value and convert into string*/       qWarning() << tr("QJsonObject[appName] of description: ") << item["description"];       QJsonValue subobj = item["description"];       qWarning() << subobj.toString();        /* in case of array get array and convert into 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 from string

Assign json to string as below and use the 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"  
like image 99
Umasankar Natarajan Avatar answered Oct 15 '22 18:10

Umasankar Natarajan