PDA

View Full Version : QScriptEngine Printout to String as JSON



ManuMies
20th May 2010, 14:18
I'm using QScriptEngine to create JSON object like this.




QStringList aApplicatoinIds;
QStringList aDisplaynames;
QStringList aIconPaths;
QList<int> aMessageCounts;
aApplicatoinIds.append("app.test.com");
aDisplaynames.append("testi appi");
aIconPaths.append("c:\\data\\icon2.bmp");
aMessageCounts.append(2);
aApplicatoinIds.append("app.demo.com");
aDisplaynames.append("demo appi");
aIconPaths.append("c:\\data\\demo.bmp");
aMessageCounts.append(4);
int size = aApplicatoinIds.count();


QScriptEngine engine;
QScriptValue list = engine.newArray(size);

for (int i=0; i < size; i++)
{
QScriptValue application = engine.newObject();
application.setProperty(JSON_APPLICATIONID,aApplic atoinIds.at(0));
application.setProperty(JSON_DISPLAYNAME,aDisplayn ames.at(0));
application.setProperty(JSON_ICONPATH,aIconPaths.a t(0));
application.setProperty(JSON_MESSAGES,aMessageCoun ts.at(0));
list.setProperty(i,application);
LOGTXT("++");
}

QScriptValue notify = engine.newObject();
notify.setProperty(JSON_LIST,list);

QScriptValue globalObj = engine.newObject();
globalObj.setProperty(JSON_NOTIFY,notify);

engine.setGlobalObject(globalObj);



Now, I'¨m trying to get engine output as JSON in QString like this:


{
"notify":{
"list": [
{
"applicationid":"test.app.com",
"displayname":"testing",
"iconpath":"c:\data\test.img",
"messages":5
}
]}
}


How to do it?

Or if you can show some code example how to create JSON string, I'm listening. :)

ComaWhite
20th May 2010, 23:10
I'd take a look at QJSON

ManuMies
21st May 2010, 09:24
That's the first thing I found about JSON handling in Qt, but the problem is that it's under license and I don't want to go publishing my own codes so it's not an option for me.

Any other ideas? Anyway to get the QScriptEngine exporing the whole JSON as string?