QT——对无键值的json数据解析

看代码,应该都能看懂

json文件

[
    {
        "recordId": "1ead6ee3-92c6-41dd-8a80-9447e33d6e96",
        "createAt": "2021-10-21 15:34:16",
        "status": "None",
        "aActivity": 0.2,
        "bActivity": 0.3,
        "gammaCpsList": [
            1.18,
            0.33,
            1.51,
            0.22
        ],
        "doseRateList": [
            0.1,
            0.19,
            1.74,
            1.0
        ]
    },
    {
        "recordId": "8a7d45d3-5637-4c69-bb15-37ca6213fa0b",
        "createAt": "2021-10-21 15:34:06",
        "status": "None",
        "aActivity": 0.2,
        "bActivity": 0.1,
        "gammaCpsList": [
            0.75,
            1.18,
            1.39,
            0.63
        ],
        "doseRateList": [
            0.55,
            0.21,
            0.28,
            1.93
        ]
    }
]
struct gammaDoseRate
{
	QString name;
	double id_1;
	double id_2;
	double id_3;
	double id_4;
	gammaDoseRate() {}
	gammaDoseRate(QString _name,double _id_1, double _id_2, double _id_3, double _id_4)
	{
		name = _name;
		id_1 = _id_1;
		id_2 = _id_2;
		id_3 = _id_3;
		id_4 = _id_4;
	}
};

gammaDoseRateList是一个容器(QVector gammaDoseRateList;)

	QJsonObject objs = jsonDocument.object();
	QByteArray byteArray = jsonDocument.toJson(QJsonDocument::Compact);
	QString strJson(byteArray);

	auto json_doc = QJsonDocument::fromJson(byteArray);
	QJsonArray json_array = json_doc.array();
	QVariantList root_map = json_array.toVariantList(); // This is a list, not a map!

	QVariantMap stat_map = root_map.at(0).toMap();
	QVariantMap stat_map2 = root_map.at(1).toMap();
	QVariantMap stat_map3 = root_map.at(2).toMap();
	for (int i = 0; i < root_map.size(); i++)
	{
		gammaDoseRate gammaDose;
		QVariantMap stat_map = root_map.at(i).toMap();
		QStringList key_list = stat_map.keys();
		for (int j = 0; j < key_list.size(); j++)
		{
			if (key_list[j] == "name")
			{
				gammaDose.name = stat_map[key_list[j]].toString();
			}
			if (key_list[j] == "gammaDoseRate")
			{
				QStringList rate = stat_map[key_list[j]].toStringList();
				gammaDose.id_1 = rate[0].toDouble();
				gammaDose.id_2 = rate[1].toDouble();
				gammaDose.id_3 = rate[2].toDouble();
				gammaDose.id_4 = rate[3].toDouble();
			}
		}
		gammaDoseRateList.push_back(gammaDose);
	}

你可能感兴趣的:(QT,json,qt,开发语言)