本文介绍了如何在QT C ++中解析嵌套的json字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好可以解释如何在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();
}

推荐答案


这篇关于如何在QT C ++中解析嵌套的json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:14