如何在QT C ++中解析嵌套的json字符串 [英] How to parse nested json string in QT C++
问题描述
您好可以解释如何在qt4.7(C ++)中解析嵌套的json响应文件。该文件位于路径/home/kkk/response.json\"。该文件包含以下response.Iam在Linux环境中使用Qt。
{respcode:200,respdesc:成功,计数:3,
nodalofficers :[
{attid:XXX,attname:XXX},
{attid: XXX,attname:XXX},
{attid:XXX,attname:XXX}]
}
我的尝试:
我试过下面的代码但在这里我不知道如何声明Jsondocument(文档)这就是为什么会出错。
QStringList countlist = document.elementsByTagName(nodalofficers);
if(countlist.isEmpty())
{
DisplayWideMsgBox(收到无效响应\ n请再试一次);
返回-1;
}
int itemCount = countlist.at(0).childNodes()。count();
qDebug()<<COUNT:<< itemCount;
list = document.elementsByTagName(nodalofficers);
if(list.isEmpty())
{
DisplayWideMsgBox(为域名列表收到的空数据);
返回-1;
}
for(int i = 0,j = 0; i< itemCount; i ++,j = 0)
{
QString attid =
list.at(i).childNodes()。at(j ).toElement()。text();
QString attname =
list.at(i).childNodes()。at(j + 1).toElement() .text();
}
Hi Can you explain how to parse the nested json response file in qt4.7 (C++). And the file is in the path "/home/kkk/response.json".The file contains the below response.Iam using Qt in Linux environment.
{ " respcode ":" 200 "," respdesc ":"success"," count ":"3",
" nodalofficers ":[
{" attid ":"XXX"," attname ":"XXX"},
{" attid ":"XXX"," attname ":"XXX"},
{" attid ":"XXX"," attname ":"XXX"}]
}
What I have tried:
I have tried below code but here I dont know how to declare Jsondocument(document) thats why getting errors.
QStringList countlist = document.elementsByTagName("nodalofficers");
if(countlist.isEmpty())
{
DisplayWideMsgBox("Invalid Response Received \nPlease Try Again");
return -1;
}
int itemCount = countlist.at(0).childNodes().count();
qDebug()<<"COUNT :"<<itemCount;
list = document.elementsByTagName("nodalofficers");
if(list.isEmpty())
{
DisplayWideMsgBox("Empty Data Received for domain list");
return -1;
}
for(int i = 0,j = 0;i < itemCount;i++,j = 0)
{
QString attid =
list.at(i).childNodes().at(j).toElement().text();
QString attname =
list.at(i).childNodes().at(j+1).toElement().text();
}
推荐答案
Qt5引入了JSON支持( Qt支持Qt | Qt Core 5.11 [ ^ ])。
如果你坚持相当陈旧(2010 - 2012年)而不是不再支持(自2015年起)4.7版本,我建议使用C ++ JSON库而不是自己尝试。 Qt甚至还有一个: GitHub - flavio / qjson:QJson是一个基于qt的库,可以将JSON数据映射到QVariant对象。 [ ^ ]。
JSON support has been intruduced with Qt5 (JSON Support in Qt | Qt Core 5.11[^]).
If you are stuck to the rather old (2010 - 2012) and not anymore supported (since 2015) 4.7 version, I suggest to use a C++ JSON library instead of trying to do it yourself. There is even one for Qt: GitHub - flavio/qjson: QJson is a qt-based library that maps JSON data to QVariant objects.[^].
这篇关于如何在QT C ++中解析嵌套的json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!