Hi,
I’m doing a request to an url which gives me the next Json reply:
"{"data":"[{\"Name\":\"One\",\"Code\":\"\"},{\"Name\":\"Two\",\"Code\":\"Alpha\"}]"}"
"{"data":"[{\"Name\":\"One\",\"Code\":\"\"},{\"Name\":\"Two\",\"Code\":\"Alpha\"}]"}"
To copy to clipboard, switch view to plain text mode
I am using Qt 8.4.5 so I have to do it with QNetwork and QScript. So following an example I saw (Example)I did the next:
QNetworkRequest request(_Url);
QNetworkReply *reply = nam->get(request);
if ( reply->error()== QNetworkReply::NoError)
{
//------------ JSON Parser --------------//
QScriptEngine engine;
QScriptValue result = engine.evaluate("("+InfoByteArray+")");
QScriptValue InfoData= result.property("data");
qDebug() << "InfoData" << InfoData.toString();
}
QNetworkRequest request(_Url);
QNetworkReply *reply = nam->get(request);
QString InfoByteArray = (QString)reply->readAll();
if ( reply->error()== QNetworkReply::NoError)
{
//------------ JSON Parser --------------//
QScriptEngine engine;
QScriptValue result = engine.evaluate("("+InfoByteArray+")");
QScriptValue InfoData= result.property("data");
qDebug() << "InfoData" << InfoData.toString();
}
To copy to clipboard, switch view to plain text mode
With that I get all the part on the right of “dataâ€:
[{"Name":"One","Code":""},{"Name":"Two","Code":"Alpha"}]"
[{"Name":"One","Code":""},{"Name":"Two","Code":"Alpha"}]"
To copy to clipboard, switch view to plain text mode
Now I want to take the Name en Code values ofc but I can’t get them, I try doing this but never goes into the while:
QScriptValueIterator it(InfoData);
while (it.hasNext())
{
it.next();
QScriptValue entry = it.value();
QString name
= entry.
property("Name").
toString();
//this doesnt works QString name2
= entry.
property("data").
property("Name").
toString();
//this either }
QScriptValueIterator it(InfoData);
while (it.hasNext())
{
it.next();
QScriptValue entry = it.value();
QString name= entry.property("Name").toString(); //this doesnt works
QString name2= entry.property("data").property("Name").toString(); //this either
}
To copy to clipboard, switch view to plain text mode
And if I try doing that out of the while, I just get empty strings :
QString name
= result.
property("Name").
toString();
// qDebug() << name; returns "" QString name2
= entry.
property("data").
property("Name").
toString();
QString name= result.property("Name").toString(); // qDebug() << name; returns ""
QString name2= entry.property("data").property("Name").toString();
To copy to clipboard, switch view to plain text mode
So…Any Idea of what am I doing wrong here so I can't get the Name or the Code values??
Thank you!
Bookmarks