JSON - Qt 对 JSON的处理
1.关于JSON的介绍参考www.json.org
Qt提供处理JSON数据的支持。
QJSonObject类用于封装JSON object;
QJsonDocument类提供读写JSON文档的方法;
QJsonParseError类用于在JSON解析过程中报告错误。
上述三个类均是从Qt 5.0开始支持。
示例:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonParseError>
#include<QDebug> MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this); QJsonObject json;
json.insert("Type",QString("Rectangle"));
json.insert("widght",);
json.insert("height",); QJsonDocument document;
document.setObject(json);
QByteArray byteArray = document.toJson(QJsonDocument::Compact);
qDebug()<<byteArray; QJsonParseError jsonError;
QJsonDocument parseDoc = QJsonDocument::fromJson(byteArray,&jsonError);
if(jsonError.error == QJsonParseError::NoError)
{
if(parseDoc.isObject())
{
QJsonObject jsonObj = parseDoc.object();
if(jsonObj.contains("Type"))
{
QJsonValue typeValue = jsonObj.take("Type");
if(typeValue.isString())
{
QString strValue= typeValue.toString();
qDebug()<<"Type : "<<strValue;
}
}
if(jsonObj.contains("height"))
{
QJsonValue heightValue = jsonObj.take("height");
if(heightValue.isDouble())
{
int iValue = heightValue.toVariant().toInt();
qDebug()<<"height : "<<iValue;
}
}
if(jsonObj.contains("widght"))
{
QJsonValue widghtValue = jsonObj.take("widght");
if(widghtValue.isDouble())
{
int iValue =widghtValue.toVariant().toInt();
qDebug()<<"widght : "<<iValue;
}
}
}
}
} MainWindow::~MainWindow()
{
delete ui;
}
结果如下:
"{\"Type\":\"Rectangle\",\"height\":23,\"widght\":42}"
Type : "Rectangle"
height :
widght :
JSON - Qt 对 JSON的处理的更多相关文章
- Qt之JSON生成与解析
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - December ...
- Qt读取JSON和XML数据
QJSON JSON(JavaScript Object Notation)是一个轻量级的数据交换格式; 可以将数据以name/value的形式任意组合; QJson 是一个基于Qt的库, 将JSON ...
- Qt浅译:JSON Support in Qt(JSON只有六种数据类型)
JSON Support in Qt Qt5之后开始提供对处理JSON数据的支持,JSON是一种Interter数据交换的数据格式. JSON 用于存储结构化的数据,JSON有6种基本数据类型 ...
- 【转】Qt之JSON保存与读取
简述 许多游戏提供保存功能,使得玩家在游戏中的进度可以被保存,并在以后再玩的时候进行加载.保存游戏的过程通常涉及将每个游戏对象的成员变量序列化为文件.要实现这个功能,可以采取许多格式,其中之一就是 J ...
- 【转载】Qt之JSON生成与解析
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - December ...
- Qt解析Json数据
1 JSON数据简介 JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式.它基于 ECMAScript (欧洲计算机协会制定的js规范) ...
- QJsonDocument实现Qt下JSON文档读写
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QJsonDocument实现Qt下JSON文档读写 本文地址:http://tech ...
- Qt读写Json
Qt操作Json 1.QJsonDocument 1.详细说明 QJsonDocument类提供了读写JSON文档的方法. QJsonDocument是一个封装了完整JSON文档的类,可以从基于UTF ...
- .Net使用Newtonsoft.Json.dll(JSON.NET)对象序列化成json、反序列化json示例教程
JSON作为一种轻量级的数据交换格式,简单灵活,被很多系统用来数据交互,作为一名.NET开发人员,JSON.NET无疑是最好的序列化框架,支持XML和JSON序列化,高性能,免费开源,支持LINQ查询 ...
随机推荐
- JDBC操作数据时中文乱码
/** * DB地址 */ private static final String DB_URL="jdbc:mysql://localhost:3306/db_book?useUnicod ...
- 关于Java在Linux or Android平台调用.so库
Linux平台Java调用so库-JNI使用例子 android NDK开发及调用标准linux动态库.so文件 在Android项目中调用已有.so库 Android 调用.so文件 jni And ...
- webstorm 激活码
WebStorm 2016 最新版激活(activation code方式) 注册时,在打开的License Activation窗口中选择“activation code”,在输入框输入下面的注册码 ...
- UVa 10945 - Mother bear
题目大意:给一个字符串,判断是否回文(忽略大小写,忽略非字母字符). #include <cstdio> #include <cctype> #include <cstr ...
- python获取绑定的IP,并动态指定出口IP
在做采集器的过程中,经常会遇到IP限制的情况,这时候可以通过切换IP能继续访问. 如果是多IP的服务器,那么可以通过切换出口Ip来实现. 1.首先是如何获取服务器绑定的IP import netifa ...
- oracle系列--解锁数据库
一.安装完成后解锁Scott数据库步骤: 进入SQL Plus 请输入用户名:sys输入口令:sys as sysdba //这里的口令是不可见的,注意空格SQL> alter user sc ...
- php sprintf函数
定义和用法 sprintf() 函数把格式化的字符串写写入一个变量中.sprintf(format,arg1,arg2,arg++) 参数 描述 format 必需.转换格式. arg1 必需.规定插 ...
- 2.13.1. 对结果排序(Core Data 应用程序实践指南)
传递NSSortDescriptor给NSFetchRequest进行排序.示例如下,修改demo方法: NSSortDescriptor *sort = [NSSortDescriptor sort ...
- Java:网络编程
一.因特网地址 InetAddress类:实现主机名和因特网地址之间的转换. InetAddress address=InetAddress.getByName(String);返回一个InetAdd ...
- 告别被拒,如何提升iOS审核通过率(下篇)——应用内容检查大法与提审资源检查大法
WeTest 导读 之前的<告别被拒,如何提升iOS审核通过率(上篇)>分享了客户端检查的相关要点,本篇会给大家介绍有关应用内容的检查项和提审资源相关检查项要点. 应用内容检查大法 苹果对 ...