Qt-JSON的使用

http://blog.csdn.net/wangshubo1989/article/details/52688451

关于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
  }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

使用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");
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

得到字段

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();
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

使用QJsonDocument

            QJsonParseError jsonError;
            QJsonArray json_array = QJsonDocument::fromJson(data, &jsonError).array();
            if(jsonError.error == QJsonParseError::NoError) {
                QList<QPair<QString,QString>> 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;
            }
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
应用–序列化一个JSON:
QtJson::JsonObject contributor;
contributor["name"] = "Luis Gustavo";
contributor["age"] = 22;

QByteArray data = QtJson::serialize(contributor);
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5
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;
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

构建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);
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值