A very easy to use Qt library for Writing and Reading Json Data.
Here is an example which should explain the basic use cases:
#include "jsonobject.h"
int main()
{
JsonObject main;
// array
main["array"] = QList<QString> {"Element 1", "Element 2", "Element 3"};
main["array"]() = true;
// object
main["object"] = QMap<QString, QString> {{"Key 1", "Value 1"}, {"Key 2", "Value 2"}};
main["object"]["key"] = "value";
// integer
main["integer"] = 12;
// string
main["string"] = "My String";
// null
main["null"];
// set via path
*main.path("this.is.my.path.0") = 12;
// access value
qDebug("this.is.my.path = %d\n", main.path("this.is.my.path.0")->integer());
// copy to other object
JsonObject second;
second["Second"] = main;
// just print results
for(int i = 0; i < 2; i++)
{
printf("Print Output of %s JsonObject:\n%s\n\n",
!i ? "Main" : "Second",
qPrintable((!i ? main : second).toJson(JsonObject::Pretty)));
}
return 0;
}
This example produces the following output:
this.is.my.path = 12
Print Output of Main JsonObject:
{
"array": [
"Element 1",
"Element 2",
"Element 3",
true
],
"integer": 12,
"null": null,
"object": {
"Key 1": "Value 1",
"Key 2": "Value 2",
"key": "value"
},
"string": "My String",
"this": {
"is": {
"my": {
"path": [
12
]
}
}
}
}
Print Output of Second JsonObject:
{
"Second": {
"array": [
"Element 1",
"Element 2",
"Element 3",
true
],
"integer": 12,
"null": null,
"object": {
"Key 1": "Value 1",
"Key 2": "Value 2",
"key": "value"
},
"string": "My String",
"this": {
"is": {
"my": {
"path": [
12
]
}
}
}
}
}
Just add the following to your Qt-Project file:
include(jsonobject.pri)
Include project syntax:
#include "jsonobject.h"
Note: The make install installation pathes, are printed to you during qmake!
qmake jsonobject
make
make install
add the following to your pro file:
LIBS += -ljsonobject
Include project syntax:
#include <jsonobject/jsonobject.h>
The jsonobject licence is a modified version of the LGPL licence, with a static linking exception.