Qt 解释JSon数据
阅读原文时间:2023年07月17日阅读:3

jsondata={"cmd":"meetingmemberlist","memberinfos":[{"name":"李","number":"668162","status":"Offline"},{"name":"66668888","status":"Offline"},{"name":"1.221","number":"1234561","status":"Offline"},{"name":"1.42","status":"Offline"}],"session":"2989479764ll","touid":"19","xns":"18"}

void CEMViewMenuInteraction::get_memberdata_from_jsondate(std::string jsondata)
{
QByteArray ByteArr = QByteArray(jsondata.c_str());
//判断字符串转化为 QJsonDocument 是否出现了错误
QJsonParseError jsonError;//Qt5新类
QJsonDocument jsonDoc = QJsonDocument::fromJson(ByteArr, &jsonError);//Qt5新类
if (jsonError.error == QJsonParseError::NoError)
{
// qDebug()<< "parse jsondata";
if (jsonDoc.isObject())
{
QJsonObject rootObj = jsonDoc.object();
QString cmd;
QString session;
//是否含有key cmd
if (rootObj.contains("cmd"))
{
//取出key为rootpath的值
QJsonValue value = rootObj.value("cmd");
//判断是否是string类型
if (value.isString())
{
cmd = value.toString();
// qDebug()<< "cmd=" <<cmd;
}
}
if (rootObj.contains("session"))
{
//取出key为 session 的值
QJsonValue value = rootObj.value("session");
//判断是否是string类型
if (value.isString())
{
session = value.toString();
// qDebug()<< "session=" << session;
}
}
if (rootObj.contains("memberinfos"))
{
QJsonValue valueArray = rootObj.value("memberinfos");
//判断类型是否为array,并且将array遍历出来
if (valueArray.isArray())
{
QJsonArray jsonArray = valueArray.toArray();
QString child_name = "null";
QString child_status = "null";
for (int i = ; i < jsonArray.count();i++)
{
QJsonValue childValue = jsonArray[i];
if (childValue.isObject())
{
QJsonObject childObject = childValue.toObject();
if (childObject.contains("name"))
{
QJsonValue valueJson = childObject.value("name");
if (valueJson.isString())
{
child_name = valueJson.toString();
// qDebug()<< "name=" <<child_name;
}
}
if (childObject.contains("status"))
{
QJsonValue valueJson = childObject.value("status");
if (valueJson.isString())
{
child_status = valueJson.toString();
// qDebug()<< "status=" <<child_status;
}
}
m_MemberListMap[child_name] = child_status;
}
}
}
}

   }  

}
}