Click here to Skip to main content
15,911,132 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
}
Posted
Updated 18-Jul-18 23:03pm

1 solution

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.[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900