#include "widget.h"
#include <QApplication>
#include <QCoreApplication>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonValue>
#include <QString>
#include <QStringList>
#include <QDebug>
/**************************
* QJson 解析基本流程
* 字符串——》QJsonDocument——》QJson对象/QJson数组——》解析QJson对象
*/
void parseJson_1()
{
QString json("{\"name\":\"scorpio\", \"year\":2016, \"array\":[30, \"hello\", true]}");
QJsonParseError jsonerror;
// 转换字符串为 QJsonDocument 对象
QJsonDocument doc = QJsonDocument::fromJson(json.toLatin1(), &jsonerror);
if (!doc.isNull() && jsonerror.error == QJsonParseError::NoError)
{
// 判断内容是Json数组还是Json对象
if(doc.isObject()) {
// 将 doc 转换为一个 Json 对象
QJsonObject object = doc.object();
// 取出 Json对象的键列表
QStringList list = object.keys();
for(int i = 0; i < list.count(); i++) {
qDebug() << list.at(i);
}
// 返回指向JSON对象的第一个元素的STL风格的迭代器
QJsonObject::iterator it = object.begin();
while(it != object.end()) {
// 解析Json元素Key的类型
switch(it.value().type()) {
case QJsonValue::String: {
qDebug() << "Type is string";
qDebug() << it.key() << "=" << it.value().toString();
break;
}
case QJsonValue::Array: {
qDebug() << "Type is Array";
qDebug() << it.key() << "=" << it.value().toArray();
QJsonArray sub = it.value().toArray();
// 取出数组的每一个元素
qDebug() << "index 1:" << sub.at(0).toDouble();
qDebug() << "index 2:" << sub.at(1).toString();
qDebug() << "index 3:" << sub.at(2).toBool();
break;
}
case QJsonValue::Bool: {
qDebug() << "Type is Bool";
qDebug() << it.key() << "=" << it.value().toBool();
break;
}
case QJsonValue::Double: {
qDebug() << "Type is Double";
qDebug() << it.key() << "=" << it.value().toDouble();
break;
}
case QJsonValue::Object: {
qDebug() << "Type is Object";
qDebug() << it.key() << "=" << it.value().toObject();
break;
}
case QJsonValue::Null: {
qDebug() << "Type is Null";
qDebug() << it.key() << "= NULL" ;
break;
}
case QJsonValue::Undefined: {
qDebug() << "Type is Undefined";
break;
}
}
it++;
}
}
}
}
/**************************
* QJson对象构造、QJson文档构造
*/
void parseJson_2()
{
/***
* 构建JSON对象
*/
QJsonObject json;
json.insert("Name", "Qt");
json.insert("From", 2016);
json.insert("Cross Platform", true);
/***
* 构建JSON文档
*/
QJsonDocument document;
document.setObject(json);
// QJson对象转换到QByteArray
QByteArray byteArray = document.toJson();
QString strJson(byteArray);
qDebug() << strJson;
/***
* 解析 Json
*/
QJsonParseError jsonError;
// 还是通过字符串流构建Json文档
QJsonDocument doucment = QJsonDocument::fromJson(byteArray, &jsonError);
if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError))
{
if (doucment.isObject()) {
// 还是Json文档转换为 Json 对象
QJsonObject object = doucment.object();
// 判断Json对象是否包含键 Name
if (object.contains("Name"))
{
// 包含指定的 key
QJsonValue value = object.value("Name");
if (value.isString()) { // 判断 value 是否为字符串
QString strName = value.toString();
qDebug() << "Name : " << strName;
}
}
if (object.contains("From"))
{
QJsonValue value = object.value("From");
if (value.isDouble())
{
int nFrom = value.toVariant().toInt();
qDebug() << "From : " << nFrom;
}
}
if (object.contains("Cross Platform"))
{
QJsonValue value = object.value("Cross Platform");
if (value.isBool())
{
bool bCrossPlatform = value.toBool();
qDebug() << "CrossPlatform : " << bCrossPlatform;
}
}
}
}
}
/**************************
* QJson 数组
*/
void parseJson_3()
{
/***
* 构建 Json 数组
*/
QJsonArray json;
// 追加 QJsonValue
json.append("Qt");
json.append(5.7);
json.append(true);
/***
* 构建 Json 文档
*/
QJsonDocument document;
// 前面是使用QJson对象构造Json文档,
// 这里使用QJson数组构造Json文档
document.setArray(json);
// 将 JSon数组 里的Json数组转换为字符串流
QByteArray byteArray = document.toJson(QJsonDocument::Compact);
QString strJson(byteArray);
qDebug() << strJson;
QJsonParseError jsonError;
QJsonDocument doucment = QJsonDocument::fromJson(byteArray, &jsonError);
if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError))
{
if (doucment.isArray()) {
// 获得 Json 数组
QJsonArray array = doucment.array();
int nSize = array.size();
for (int i = 0; i < nSize; ++i)
{
QJsonValue value = array.at(i);
if (value.type() == QJsonValue::String) {
QString strName = value.toString();
qDebug() << strName;
}
if (value.type() == QJsonValue::Double)
{
double dVersion = value.toDouble();
qDebug() << dVersion;
}
if (value.type() == QJsonValue::Bool)
{
bool bCrossPlatform = value.toBool();
qDebug() << bCrossPlatform;
}
}
}
}
}
/**************************
* 复杂 Json
* {
* "Company": "Digia",
* "From": 1991,
* "Name": "Qt",
* "Page": {
* " Developers": "https://www.qt.io/developers/",
* " Download": "https://www.qt.io/download/",
* " Home": "https://www.qt.io/"},
* "Version": [
* 4.8,
* 5.2,
* 5.7
* ]
* }
*/
void parseJson_4()
{
/***
* 构建 Json 数组 - Version
*/
QJsonArray versionArray;
versionArray.append(4.8);
versionArray.append(5.2);
versionArray.append(5.7);
/***
* 构建 Json 对象 - Page
*/
QJsonObject pageObject;
pageObject.insert("Home", "https://www.qt.io/");
pageObject.insert("Download", "https://www.qt.io/download/");
pageObject.insert("Developers", "https://www.qt.io/developers/");
/***
* 构建 Json 对象
*/
QJsonObject json;
json.insert("Name", "Qt");
json.insert("Company", "Digia");
json.insert("From", 1991);
// 值是Json对象
json.insert("Page", QJsonValue(pageObject));
// 值是Json数组
json.insert("Version", QJsonValue(versionArray));
/***
* 构建 Json 文档
*/
QJsonDocument document;
document.setObject(json);
QByteArray byteArray = document.toJson(QJsonDocument::Compact);
QString strJson(byteArray);
qDebug() << strJson;
//解析JSON
QJsonParseError jsonError;
QJsonDocument doucment = QJsonDocument::fromJson(byteArray, &jsonError);
if (!doucment.isNull() && (jsonError.error == QJsonParseError::NoError)) {
if (doucment.isObject()) {
// 拿到 JSon 对象
QJsonObject object = doucment.object();
if (object.contains("Name")) {
QJsonValue value = object.value("Name");
if (value.isString()) {
QString strName = value.toString();
qDebug() << "Name : " << strName;
}
}
if (object.contains("Company"))
{
QJsonValue value = object.value("Company");
if (value.isString()) {
QString strCompany = value.toString();
qDebug() << "Company : " << strCompany;
}
}
if (object.contains("From")) {
QJsonValue value = object.value("From");
if (value.isDouble()) {
int nFrom = value.toVariant().toInt();
qDebug() << "From : " << nFrom;
}
}
if (object.contains("Version")) {
QJsonValue value = object.value("Version");
if (value.isArray()) {
// 获取到 Json 数组
QJsonArray array = value.toArray();
int nSize = array.size();
for (int i = 0; i < nSize; ++i) {
QJsonValue value = array.at(i);
if (value.isDouble()) {
double dVersion = value.toDouble();
qDebug() << "Version : " << dVersion;
}
}
}
}
if (object.contains("Page")) {
QJsonValue value = object.value("Page");
if (value.isObject()) {
// 获取到 Json 对象
QJsonObject obj = value.toObject();
if (obj.contains("Home")) {
QJsonValue value = obj.value("Home");
if (value.isString()) {
QString strHome = value.toString();
qDebug() << "Home : " << strHome;
}
}
if (obj.contains("Download")) {
QJsonValue value = obj.value("Download");
if (value.isString()) {
QString strDownload = value.toString();
qDebug() << "Download : " << strDownload;
}
}
if (obj.contains("Developers")) {
QJsonValue value = obj.value("Developers");
if (value.isString()) {
QString strDevelopers = value.toString();
qDebug() << "Developers : " << strDevelopers;
}
}
}
}
}
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// parseJson_1();
// parseJson_2();
// parseJson_3();
parseJson_4();
return a.exec();
}