用到的头文件
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
json解析
1.
QByteArray jsonData = "[{\"id\":\"11\", \"name\":\"aaa\"}," "{\"id\":\"22\", \"name\":\"bbb\"}," "{\"id\":\"33\", \"name\":\"ccc\"}]"; QJsonParseError jsonError; QJsonDocument jsonDoucment = QJsonDocument::fromJson(jsonData, &jsonError); if(jsonError.error == QJsonParseError::NoError){ if(!(jsonDoucment.isNull()||jsonDoucment.isEmpty())){ QVariantList dataList=jsonDoucment.toVariant().toList(); foreach (QVariant data, dataList) { QVariantMap dataMap=data.toMap(); qDebug()<<"id:"<<dataMap["id"].toString()<<"name:"<<dataMap["name"].toString(); } } }else{ //检查错误类型 qDebug()<<"jsonError:"<<jsonError.errorString(); }
时间: 2024-10-05 23:56:34