Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 4f054c93d1
..
Octocat-spinner-32 AUTHORS updated qt-json March 22, 2012
Octocat-spinner-32 LICENSE updated qt-json March 22, 2012
Octocat-spinner-32 README updated qt-json March 22, 2012
Octocat-spinner-32 json.cpp updated qt-json March 22, 2012
Octocat-spinner-32 json.h updated qt-json March 22, 2012
Octocat-spinner-32 qt-json.pri renamed json to qt-json March 22, 2012
README
########################################################################
1. INTRODUCTION

The Json class is a simple class for parsing JSON data into a QVariant 
hierarchies. Now, we can also reverse the process and serialize
QVariant hierarchies into valid JSON data.


########################################################################
2. HOW TO USE

The parser is really easy to use. Let's say we have the following 
QString of JSON data:

------------------------------------------------------------------------
{
   "encoding" : "UTF-8",
   "plug-ins" : [
      "python",
      "c++",
      "ruby"
   ],
   "indent" : {
      "length" : 3,
      "use_space" : true
   }
}
------------------------------------------------------------------------

We would first call the parse-method:

------------------------------------------------------------------------
//Say that we're using the QtJson namespace
using namespace QtJson;
bool ok;
//json is a QString containing the JSON data
QVariantMap result = Json::parse(json, ok).toMap();

if(!ok) {
    qFatal("An error occurred during parsing");
    exit(1);
}
------------------------------------------------------------------------

Assuming the parsing process completed without errors, we would then
go through the hierarchy:

------------------------------------------------------------------------
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();
------------------------------------------------------------------------

The previous code would print out the following:

------------------------------------------------------------------------
encoding: "UTF-8"
plugins:
  - "python"
  - "c++"
  - "ruby"
length: 3
use_space: true
------------------------------------------------------------------------

To write JSON data from Qt object is as simple as parsing:

------------------------------------------------------------------------
QVariantMap map;
map["name"] = "Name";
map["age"] = 22;

QByteArray data = Json::serialize(map);
------------------------------------------------------------------------

The byte array 'data' contains valid JSON data:

------------------------------------------------------------------------
{
        name: "Luis Gustavo",
        age: 22,
}
------------------------------------------------------------------------


########################################################################
4. CONTRIBUTING

The code is available to download at GitHub. Contribute if you dare!
Something went wrong with that request. Please try again.