教程集 www.jiaochengji.com
教程集 >  脚本编程  >  java  >  正文 QT5 JSON与QMAP的相互转换的例子

QT5 JSON与QMAP的相互转换的例子

发布时间:2016-10-22   编辑:jiaochengji.com
教程集为您提供QT5 JSON与QMAP的相互转换的例子等资源,欢迎您收藏本站,我们将为您提供最新的QT5 JSON与QMAP的相互转换的例子资源
下面来看一仿QT5 JSON与QMAP的相互转换的例子,希望这个例子可以让各位更深入到了解到JSON与QMAP属性。
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8831')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8831>#include <QCoreApplication>
#include <QDebug>
#include <QJsonDocument>
#include <QByteArray>
#include <QVariant>
#include <QMap>
#include <QString>
#include <QJsonObject>
int main(int argc, char *argv[])
{
 QCoreApplication a(argc, argv);
 //对象转json字符串
 QMap<QString, QVariant> newData = QMap<QString, QVariant>();
 newData.insert("content", "i'm content");
 newData.insert("number", "i'm number");
 newData.insert("time", "i'm time");
 QJsonDocument doc=QJsonDocument::fromVariant(QVariant(newData));
 QByteArray jba=doc.toJson();
 QString jsonString = QString(jba);
 qDebug() << "QMap<QString, QVariant>转环后的json字符串\n" << jsonString;
 //字符串转json对象
 QByteArray njba = jsonString.toUtf8();
 QJsonObject nobj = QJsonObject(QJsonDocument::fromJson(njba).object());
 qDebug() << "字符串转换后的对象\n"
 << nobj.take("content").toString() << endl
 << nobj.take("number").toString() << endl
 << nobj.take("time").toString() << endl;
 return a.exec();
}</td></tr></table>

您可能感兴趣的文章:
QT5 JSON与QMAP的相互转换的例子
Golang的 Json string和Map互相转换
Golang json string和Map互相转换
Go 学习笔记 09 | Golang 结构体与 JSON 互相转换
php的json格式和js跨域调用的代码
php数组与Json转换的方法探讨
python2.7支持pyqt5么
解析JSON对象与字符串之间的相互转换
HTML5中使用json对象的实例代码
js读取并解析JSON类型数据

[关闭]
~ ~