当前位置: 首页 > 工具软件 > Qjson > 使用案例 >

QJson的QJsonObject使用示例说明

谭彦
2023-12-01

使用示例1的读写说明

写入Json

QByteArray JsonObjectTest::onJsonTestWrite_1()
{
	QJsonObject sceneJson;

	sceneJson.insert(QString("id"), QVariant(123).toJsonValue());
	sceneJson.insert(QString("name"), QVariant(QString("LiHua")).toJsonValue());

	QJsonObject subObject;
	subObject.insert(QString("game"), QVariant(QString("cs")).toJsonValue());
	subObject.insert(QString("sport"), QVariant(QString("run")).toJsonValue());

	sceneJson.insert(QString("Specialty"), subObject);

	QJsonDocument recordJsonDocument(sceneJson);
	QByteArray jsonShapeArray = recordJsonDocument.toJson(QJsonDocument::Indented);

	return jsonShapeArray;
}

产生的Json字符串内容如下

{
    "Specialty": {
        "game": "cs",
        "sport": "run"
    },
    "id": 123,
    "name": "LiHua"
}

读取该Json字符串

void JsonObjectTest::onJsonTestRead_1(const QByteArray& sceneArray)
{
	//QString sceneArrayString = QString(sceneArray);
	//QJsonParseError jsonError;
	//QJsonDocument recordJsonDocument = QJsonDocument::fromJson(sceneArrayString.toUtf8(), &jsonError);

	QJsonParseError jsonError;
	QJsonDocument recordJsonDocument = QJsonDocument::fromJson(sceneArray, &jsonError);
	QJsonObject sceneJsonObject = recordJsonDocument.object();

	int id;
	QString name;
	QString game;
	QString sport;

	if (sceneJsonObject.contains(QString("id"))&&(QVariant(sceneJsonObject.value(QString("id"))).canConvert<int>()))
	{
		id = QVariant(sceneJsonObject.value(QString("id"))).value<int>();
	}

	if (sceneJsonObject.contains(QString("name")) && (QVariant(sceneJsonObject.value(QString("name"))).canConvert<QString>()))
	{
		name = QVariant(sceneJsonObject.value(QString("name"))).value<QString>();
	}

	if (sceneJsonObject.contains(QString("Specialty")) && (sceneJsonObject.value(QString("Specialty")).isObject()))
	{
		QJsonObject subJsonObject = sceneJsonObject.value(QString("Specialty")).toObject();

		if (subJsonObject.contains(QString("game")) && (QVariant(subJsonObject.value(QString("game"))).canConvert<QString>()))
		{
			game = QVariant(subJsonObject.value(QString("game"))).value<QString>();
		}
		if (subJsonObject.contains(QString("sport")) && (QVariant(subJsonObject.value(QString("sport"))).canConvert<QString>()))
		{
			sport = QVariant(subJsonObject.value(QString("sport"))).value<QString>();
		}
	}
	return;
}

使用示例2的读写说明

写入Json数组

QByteArray JsonObjectTest::onJsonTestWrite_2()
{
	QJsonObject sceneJson;

	sceneJson.insert(QString("id"), QVariant(123).toJsonValue());
	sceneJson.insert(QString("name"), QVariant(QString("LiHua")).toJsonValue());

	QJsonArray subArray;

	QJsonObject subObject_1;
	subObject_1.insert(QString("game"), QVariant(QString("cs")).toJsonValue());
	subObject_1.insert(QString("sport"), QVariant(QString("run")).toJsonValue());

	QJsonObject subObject_2;
	subObject_2.insert(QString("game"), QVariant(QString("EVE")).toJsonValue());
	subObject_2.insert(QString("sport"), QVariant(QString("Basketball")).toJsonValue());

	subArray.append(subObject_1);
	subArray.append(subObject_2);

	sceneJson.insert(QString("Specialty"), subArray);

	QJsonDocument recordJsonDocument(sceneJson);
	QByteArray jsonShapeArray = recordJsonDocument.toJson(QJsonDocument::Indented);

	return jsonShapeArray;
}

生成的Json字符串如下所示

{
    "Specialty": [
        {
            "game": "cs",
            "sport": "run"
        },
        {
            "game": "EVE",
            "sport": "Basketball"
        }
    ],
    "id": 123,
    "name": "LiHua"
}

读取该Json字符串

void JsonObjectTest::onJsonTestRead_2(const QByteArray& sceneArray)
{
	//QString sceneArrayString = QString(sceneArray);
	//QJsonParseError jsonError;
	//QJsonDocument recordJsonDocument = QJsonDocument::fromJson(sceneArrayString.toUtf8(), &jsonError);

	QJsonParseError jsonError;
	QJsonDocument recordJsonDocument = QJsonDocument::fromJson(sceneArray, &jsonError);
	QJsonObject sceneJsonObject = recordJsonDocument.object();

	int id;
	QString name;
	QStringList gameList;
	QStringList sportList;

	if (sceneJsonObject.contains(QString("id")) && (QVariant(sceneJsonObject.value(QString("id"))).canConvert<int>()))
	{
		id = QVariant(sceneJsonObject.value(QString("id"))).value<int>();
	}

	if (sceneJsonObject.contains(QString("name")) && (QVariant(sceneJsonObject.value(QString("name"))).canConvert<QString>()))
	{
		name = QVariant(sceneJsonObject.value(QString("name"))).value<QString>();
	}

	if (sceneJsonObject.contains(QString("Specialty")) && (sceneJsonObject.value(QString("Specialty")).isArray()))
	{
		QJsonArray subJsonArray = sceneJsonObject.value(QString("Specialty")).toArray();
		int subJsonArraySize = subJsonArray.size();

		for (int i = 0; i < subJsonArraySize;i++) {

			QJsonValue subJsonValue = subJsonArray.at(i);

			if (subJsonValue.isObject()) {
				QJsonObject subJsonObject = subJsonValue.toObject();

				if (subJsonObject.contains(QString("game")) && (QVariant(subJsonObject.value(QString("game"))).canConvert<QString>()))
				{
					gameList.append(QVariant(subJsonObject.value(QString("game"))).value<QString>());
				}
				if (subJsonObject.contains(QString("sport")) && (QVariant(subJsonObject.value(QString("sport"))).canConvert<QString>()))
				{
					sportList.append(QVariant(subJsonObject.value(QString("sport"))).value<QString>());
				}
			}
		}
	}
	return;
}
 类似资料: