Qt-JSON的使用

关于JSON我们不用过多的进行介绍,只想说它越来越流行了。
之前写过关于JSON的文章:
1 JSON和XML的荒唐比较
http://blog.csdn.net/wangshubo1989/article/details/51277347

2 使用json11解析json
http://blog.csdn.net/wangshubo1989/article/details/51001971

当然,我们同样可以在Qt程序中使用json11进行JSON的操作,但是Qt是一个伟大的框架,我们应该尽量避免使用其他的第三方库,而使用Qt对某项功能原生的支持。

The JSON support in Qt provides an easy to use C++ API to parse, modify and save JSON data. It also contains support for saving this data in a binary format that is directly “mmap”-able and very fast to access.

JSON的格式:

bool
double
string
array
object
null

Qt中的提供的JSON相关类:

QJsonArray
Encapsulates a JSON array

QJsonDocument
Way to read and write JSON documents

QJsonObject
Encapsulates a JSON object

QJsonParseError
Used to report errors during JSON parsing

QJsonValue
Encapsulates a value in JSON

应用–解析JSON:

有一个JSON:

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

使用QtJson::parse

#include "json.h"

bool ok;
// json is a QString containing the JSON data
QtJson::JsonObject result = QtJson::parse(json, ok).toMap();

if(!ok) {
  qFatal("An error occurred during parsing");

得到字段

qDebug() << "encoding:" << result["encoding"].toString();
qDebug() << "plugins:";

foreach(QVariant plugin, result["plug-ins"].toList()) {
    qDebug() << "  -" << plugin.toString();
}

QtJson::JsonObject nested = result["indent"].toMap();
qDebug() << "length:" << nested["length"].toInt();
qDebug() << "use_space:" << nested["use_space"].toBool();

使用QJsonDocument

            QJsonParseError jsonError;
            QJsonArray json_array = QJsonDocument::fromJson(data, &jsonError).array();
            if(jsonError.error == QJsonParseError::NoError) {
                QList> servers;
                servers.append(qMakePair(QString("Closest server"), QString("closest")));
                for(int i = 0; i < json_array.size(); ++i) {
                       QJsonObject json = json_array.at(i).toObject();
                       servers.append(qMakePair(json.value("label").toString(), json.value("region").toString()));
                   }
                this->getServerListCallback(true, servers);
                return;
            }

应用–序列化一个JSON:

QtJson::JsonObject contributor;
contributor["name"] = "Luis Gustavo";
contributor["age"] = 22;

QByteArray data = QtJson::serialize(contributor);
QtJson::JsonObject friend1, friend2, friend3;
friend1["id"] = 1;
friend1["name"] = "Mackenzie Hamphrey";

friend2["id"] = 2;
friend2["name"] = "Melanie Molligan";

friend3["id"] = 3;
friend3["name"] = "Sydney Calhoun";

QtJson::JsonArray friends;
friends.append(friend1);
friends.append(friend2);
friends.append(friend3);

QtJson::JsonObject obj;
obj["friends"] = friends;

构建JSON,转为QByteArray

  QVariantMap app;
  app.insert("package_name", getGamePackageName(game_));
  app.insert("label", game_);
  app.insert("platform", "iOS");

  QVariantMap device;
  QSysInfo sysInfo;
  device.insert("fingerprint", sysInfo.prettyProductName());
  device.insert("manufacturer", sysInfo.prettyProductName());
  device.insert("model", sysInfo.prettyProductName());
  device.insert("platform", "Windows");
  device.insert("platform_release", sysInfo.productVersion());
  device.insert("platform_locale", QLocale::system().name());

  QVariantMap result;
  result.insert("title", title_);
  result.insert("locale", QLocale::system().name());
  result.insert("app", app);
  result.insert("device", device);

  QJsonObject jsonObj = QJsonObject::fromVariantMap(result);
  QJsonDocument doc(jsonObj);
  QString strJson(doc.toJson(QJsonDocument::Compact));

  QByteArray byteArray;
  byteArray.append(strJson);

你可能感兴趣的:(Qt,qt,json)