PDA

View Full Version : JSON - parsing (arrays)



Tomasz
26th December 2010, 15:17
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"
}
]
}";


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;
}


Last line, when I want to parse 'data' array gives:


"[object Object],[object Object]"


How can I parse that piece of message? I don't want to use QJSON.

thanks in advance
best regards
Tomasz

squidge
26th December 2010, 18:49
http://qtwiki.org/Parsing_JSON_with_QT_using_standard_QT_library

Tomasz
29th December 2010, 13:08
Everything works. But what should I do when 'data' isn't an array but just an object:

{
"success":true,
"msg":"success",
"count":1,
"data":
{
"id":"0",
"title":"title",
"content":"message",
"type":"note",
"created_at":"2010-12-29 09:30:00",
"sender_id":"1",
"sender_name":"someone"
}
}

Should I use QScriptValueIterator too? I'm trying to use it but with no result.

thanks in advance
best regards
Tomasz