Qt解析JSON数组
·
JSON 数组在中括号中书写,以逗号分隔。
JSON 中数组值必须是合法的 JSON 数据类型(字符串, 数字, 对象, 数组, 布尔值或 null)。
比如:
[
{
"id": "1",
"name": "aa"
},
{
"id": "2",
"name": "bb"
}
]
Qt解析方式:
QByteArray array = "[{\"name\": \"aa\",\"age\": \"20\"},{\"name\": \"bb\",\"age\": \"25\"}]";
QJsonParseError error;
QJsonDocument document = QJsonDocument::fromJson(array,&error);
if(error.error != QJsonParseError::NoError)
{
qInfo()<<"parse json error"<<error.errorString();
return;
}
if(document.isNull()||document.isEmpty())
{
qInfo()<<"parse json null or empty";
return;
}
QVariantList list = document.toVariant().toList();
for(int i = 0; i<list.count(); i++)
{
QVariantMap map = list[i].toMap();
qInfo()<<map["name"].toString();
qInfo()<<map["age"].toString();
}
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)