| QJson project page | QJson home page |
QJson is a qt-based library that maps JSON data to QVariant objects.
JSON arrays will be mapped to QVariantList instances, while JSON's objects will be mapped to QVariantMap.
// create a JSonDriver instance QJson::Parser parser; bool ok; // json is a QString containing the data to convert QVariant result = parser.parse (json, &ok);
Suppose you're going to convert this JSON data:
{
"encoding" : "UTF-8",
"plug-ins" : [
"python",
"c++",
"ruby"
],
"indent" : { "length" : 3, "use_space" : true }
}
The following code would convert the JSON data and parse it:
QJson::Parser parser; bool ok; QVariantMap result = parser.parse (json, &ok).toMap(); if (!ok) { qFatal("An error occured during parsing"); exit (1); } qDebug() << "encoding:" << result["encoding"].toString(); qDebug() << "plugins:"; foreach (QVariant plugin, result["plug-ins"].toList()) { qDebug() << "\t-" << plugin.toString(); } QVariantMap nestedMap = result["indent"].toMap(); qDebug() << "length:" << nestedMap["length"].toInt(); qDebug() << "use_space:" << nestedMap["use_space"].toBool();
encoding: "UTF-8" plugins: - "python" - "c++" - "ruby" length: 3 use_space: true
mkdir build cd build cmake .. make sudo make install
svn co svn://anonsvn.kde.org/home/kde/trunk/playground/libs/qjson
Otherwise you can download source tarballs here.
|
|
hosts this site. |
Send comments to: QJson Developers |