http://qjson.sourceforge.net/usage/

Usage

This page provides a quick overview of QJson’s features:

  • parsing: from JSON to QVariant.
  • serializing: from QVariant to JSON.
  • QObject helper: dump and restore QObject’s attributes.

For more details checkout QJson’s documentation.

Parsing: from JSON to QVariant

Converting JSON’s data to QVariant instance is really simple:

1
2
3
4
5
6
7
// create a Parser 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:

1
2
3
4
5
6
7
8
9
{
"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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
QJson::Parser parser;
bool ok; QVariantMap result = parser.parse (json, &ok).toMap();
if (!ok) {
qFatal("An error occurred 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();

The output would be:

1
2
3
4
5
6
7
encoding: "UTF-8"
plugins:
- "python"
- "c++"
- "ruby"
length: 3
use_space: true

Serialization: from QVariant to JSON

QVariant objects are converted to a string containing the JSON data.

Let’s declare a QVariant object with some data to convert:

1
2
3
4
5
6
7
8
9
10
11
QVariantList people;

QVariantMap bob;
bob.insert("Name", "Bob");
bob.insert("Phonenumber", 123); QVariantMap alice;
alice.insert("Name", "Alice");
alice.insert("Phonenumber", 321); people << bob << alice;

Now it’s time to create the Serializer:

1
2
3
4
5
6
7
8
9
QJson::Serializer serializer;
bool ok;
QByteArray json = serializer.serialize(people, &ok); if (ok) {
qDebug() << json;
} else {
qCritical() << "Something went wrong:" << serializer.errorMessage();
}

The output will be:

1
2
 "[ { "Name" : "Bob", "Phonenumber" : 123 },
{ "Name" : "Alice", "Phonenumber" : 321 } ]"

It’s possible to tune the indentation level of the resulting string using the Serializer::setIndentMode() method.

QObject helper

QJson provides an helper class for dumping QObject’s attributes to a QVariant and for restoring QObject’s attributes from a QVariantMap.

Let’s define a simple class inhereted from QObject:

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
27
28
29
30
31
32
33
class Person : public QObject
{
Q_OBJECT Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(int phoneNumber READ phoneNumber WRITE setPhoneNumber)
Q_PROPERTY(Gender gender READ gender WRITE setGender)
Q_PROPERTY(QDate dob READ dob WRITE setDob)
Q_ENUMS(Gender) public:
Person(QObject* parent = 0);
~Person(); QString name() const;
void setName(const QString& name); int phoneNumber() const;
void setPhoneNumber(const int phoneNumber); enum Gender {Male, Female};
void setGender(Gender gender);
Gender gender() const; QDate dob() const;
void setDob(const QDate& dob); private:
QString m_name;
int m_phoneNumber;
Gender m_gender;
QDate m_dob;
};

Dump QObject’s attributes to JSON

The following code will serialize an instance of Person to JSON:

1
2
3
4
5
6
7
8
9
Person person;
person.setName("Flavio");
person.setPhoneNumber(123456);
person.setGender(Person::Male);
person.setDob(QDate(1982, 7, 12)); QVariantMap variant = QObjectHelper::qobject2qvariant(&person);
Serializer serializer;
qDebug() << serializer.serialize( variant);

The generated output will be:

1
{ "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }

Restore QObject’s attributes from JSON

It’s also possible to initialize a QObject using the values stored inside of a QVariantMap.

Suppose you have the following JSON data stored into a QString:

1
{ "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }

The following code will initialize an already allocated instance of Person using the JSON values:

1
2
3
4
Parser parser;
QVariant variant = parser.parse(json);
Person person;
QObjectHelper::qvariant2qobject(variant.toMap(), &person);

QJson的更多相关文章

  1. 利用QJSON将FDQuery转成JSON串

    服务器要支持Http协议,打算采用Http+JSON的方式来交换数据.一开始考虑使用superobject,因为以前使用比较多,比较熟悉. 代码如下: class function FDQueryTo ...

  2. Delphi用QJSON解析JSON格式的数据

    本来用superobject来解析JSON已经够用了,可惜这个东东不能在移动端使用,于是找到QJSON来处理. 这是一个国内高手写开源免费的东西,赞一个. 假入数据如下: {"message ...

  3. Qt 学习之路 :使用 QJson 处理 JSON

    XML 曾经是各种应用的配置和传输的首选方式.但是现在 XML 遇到了一个强劲的对手:JSON.我们可以在 这里 看到有关 JSON 的语法.总体来说,JSON 的数据比 XML 更紧凑,在传输效率上 ...

  4. Qt Linux 使用QJson库

    1. 下载 到http://dl.oschina.net/soft/qjson下载库源文件: qjson-0.8.1-1385961227890.zip 解压为:qjson-0.8.1 2. 编译 c ...

  5. QJson 的使用

    下载 源码解压 https://github.com/flavio/qjson 复制 src 目录下所有 .h .cpp .hh 文件到项目目录 qjson,pro 文件添加 INCLUDEPATH ...

  6. QT使用QJson生成解析Json数据的方法

    QT中使用json还是比较方便的,下面用例子直接说明 举例子之前首先推荐一个在线解析json格式的网站,具体格式用法如下图所示: 之后根据这个格式进行json数据解析. QT使用json需要包含的头文 ...

  7. Delphi Qjson

    使用QJSON解析数据: JSon 字符串: {"Code":1,"Msg":"", "Data":[{"Ne ...

  8. windows下QJson的编译和安装

    本文是编译安装Qjson过程中遇到的问题解决过程.主要参照网上资料和自己试验得到. 关于Qjson的用处参照:Qt 学习之路 2(63):使用 QJson 处理 JSON Qjson clone地址: ...

  9. QJSON封装好的序列和还原方法

    QJSON封装好的序列和还原方法 {*******************************************************}{ }{ QJSON与数据集互转 }{ }{ 版权所 ...

  10. Qt 学习之路 2(63):使用 QJson 处理 JSON

    Home / Qt 学习之路 2 / Qt 学习之路 2(63):使用 QJson 处理 JSON Qt 学习之路 2(63):使用 QJson 处理 JSON  豆子  2013年9月9日  Qt ...

随机推荐

  1. 关于Django项目集成Xadmin后,出现服务异常解决方案

    Django项目集成Xadmin后,偶尔出现页面不能访问,重启服务也不行,如果是Nginx部署直接报504等错误. 解决方案: 在项目中的静态文件中找到:bootstrap-clockpicker.j ...

  2. 论文解读:Knowledge Neurons in Pretrained Transformers

      论文发表于自然语言处理顶会ACL-2022(原文链接).本文引入知识神经元 (Knowledge Neuron) 的概念,初步研究了预训练Transformer中是如何存储事实知识的:   1.通 ...

  3. Jenkinsfile_配置定时任务

    triggers 在jenkinsfile来定义流水线时,常规情况下,项目都是基于手动点击部署,这种策略尤其适用于线上环境,但在测试环境,乃至于预发环境,应该对自动构建有更高的集成度,使开发者只关注于 ...

  4. 生活中有苦难却没有人可以倾述?来看看AI树洞吧!

    本文由 ChatMoney团队出品 介绍说明 在如今繁忙喧嚣的世界中,我们时常渴望能有一个安全且私密的空间,让我们毫无顾忌地袒露心声.AI 智能体树洞便是这样一个独特的存在. 它并非传统意义上的树洞, ...

  5. 【前端AI实践】简说AI大模型:什么时代了你还不会这些AI大模型的基本概念和使用吗

    如果你是前端开发者,可能已经用过像 ChatGPT.GitHub Copilot 这样的 AI 工具.它们背后都有一个核心能力在支撑:AI 大模型. 那这个"大模型"到底是个啥?它 ...

  6. 东吴证券X袋鼠云:数据轻松可取、毫秒级反应能力,东吴证券做对了什么?

    "所有事情都可以用数字表达." 这句话是1998年一部非典型金融影片<圆周率>的男主独白.影片中,天才数学家Max发现了一套数学模型在"操纵"着股票 ...

  7. 使用 Spread.net将 Excel 中的文本拆分为多段

    引言: 在 .NET 应用程序中处理 Excel 数据时,常常会碰到需要把文本拆分成多段的情况.例如在数据清洗.数据分析等场景下,将长文本按照特定规则拆分为多段能让数据更便于处理和分析.Spread. ...

  8. 32岁入行STM32迟吗?

    作为一个在嵌入式领域摸爬滚打了近10年的老兵,看到这个问题时心情五味杂陈.32岁入行STM32迟吗?说实话,如果你问我这个问题的时候我还是24岁的小白,我可能会觉得"哇,32岁才开始学单片机 ...

  9. C# 字符串 MD5 转Base64 (PHP 的编码一样)

    https://www.cnblogs.com/xiaochu/p/4402195.html MD5加密字符串并转化为base64(C#和PHP代码相同实现) -------PHP------代码-- ...

  10. SQL SERVER 查看所有触发器

    https://www.cnblogs.com/guorongtao/p/13329618.html SELECT      object_name(a.parent_obj) as [表名]     ...