Hello!
I'm trying to parse JSON. Simple JSON message is no problem, but I don't know how can I parse arrays. My code:
JSON data:
newData = {
"sucess":true,
"msg":"Success!",
"count":2,
"data":[
{
"id":"1",
"title":"first",
"type":"request",
"created_at":"2010-12-20 09:00:00",
"sender_id":"1",
"sender_name":"someone"
},
{
"id":"2",
"title":"second",
"type":"notification",
"created_at":"2010-12-21 09:00:00",
"sender_id":"1",
"sender_name":"someone"
}
]
}";
newData = {
"sucess":true,
"msg":"Success!",
"count":2,
"data":[
{
"id":"1",
"title":"first",
"type":"request",
"created_at":"2010-12-20 09:00:00",
"sender_id":"1",
"sender_name":"someone"
},
{
"id":"2",
"title":"second",
"type":"notification",
"created_at":"2010-12-21 09:00:00",
"sender_id":"1",
"sender_name":"someone"
}
]
}";
To copy to clipboard, switch view to plain text mode
Parsing code:
qDebug() << newData << endl;
QScriptValue sc;
QScriptEngine engine;
sc
= engine.
evaluate("(" + QString(newData
) + ")");
if (sc.property("sucess").toBool())
{
qDebug() << sc.property("msg").toString() << endl;
qDebug() << sc.property("count").toInteger() << endl;
qDebug() << endl << sc.property("data").toString() << endl;
}
qDebug() << newData << endl;
QScriptValue sc;
QScriptEngine engine;
sc = engine.evaluate("(" + QString(newData) + ")");
if (sc.property("sucess").toBool())
{
qDebug() << sc.property("msg").toString() << endl;
qDebug() << sc.property("count").toInteger() << endl;
qDebug() << endl << sc.property("data").toString() << endl;
}
To copy to clipboard, switch view to plain text mode
Last line, when I want to parse 'data' array gives:
"[object Object],[object Object]"
"[object Object],[object Object]"
To copy to clipboard, switch view to plain text mode
How can I parse that piece of message? I don't want to use QJSON.
thanks in advance
best regards
Tomasz
Bookmarks