1、示例Json
//测试json串
QString json_data = "{\
\"FSHC\": 1,\
\"SVSDVSS.ini\": {\
\"md5\": \"WFEWFWFWEFW\",\
\"relative_path\": \"/SFEWFW/ini/\"\
},\
\"SVSVS.jar\": {\
\"md5\":\"SGERGERGER\", \
\"relative_path\": \"/GERGERGERG/\"\
},\
\"SVSVE.ico\": {\
\"md5\": \"ASVDFBDFBDFBc\",\
\"relative_path\": \"/SVDF/DS/\"\
}\
}";
2、引用库(JsonCPP) 可自行下载(或参考附件)https://download.csdn.net/download/qq_36939187/86503343
3、解析代码
//读取Json串
Json::Value root;
Json::Reader reader;
if(!reader.parse(json_data.toStdString().c_str(), root))
{
// fail to parse
qDebug() << "'fail";
}
else // success
{
//解析是否更新
bool bUpdate = false;
bool bOffiaial = root[STR_OFFICIAL].asInt();
qDebug() << " bOffiaial = " << bOffiaial;
//解析待更新项
QMap<QString, QMap<QString, QString> > fileInfo; //解析Json文件信息
Json::Value::Members rootMembers = root.getMemberNames();
for(Json::Value::Members::iterator iter = rootMembers.begin(); iter != rootMembers.end(); ++iter)
{
if(Json::objectValue == root[*iter].type())
{
QString strRootItem = QString::fromStdString(*iter);
QMap<QString, QString> valueMap;
Json::Value::Members childMember = root[*iter].getMemberNames();
for(Json::Value::Members::iterator it = childMember.begin(); it != childMember.end(); ++it)
{
QString strChildItem = QString::fromStdString(*it);
QString strChildValue = QString::fromStdString(root[*iter][*it].asString());
valueMap.insert(strChildItem, strChildValue);
}
fileInfo.insert(strRootItem, valueMap);
}
// else if(Json::arrayValue == root[*iter].type())
// {
// //qDebug() << "Json::arrayValue = " << QString::fromStdString(root[*iter][i].asString());
// }
// else if (root[*iter].type() == Json::stringValue)
// {
// //qDebug() << QString::fromStdString(root[*iter].asString()) << endl;
// }
// else if (root[*iter].type() == Json::realValue)
// {
// //qDebug() << root[*iter].asDouble() << endl;
// }
// else if (Json::uintValue == root[*iter].type())
// {
// //qDebug() << root[*iter].asUInt();
// }
// else
// {
// //qDebug() << root[*iter].asInt() << endl;
// }
}
qDebug() << fileInfo;
}