QML and JSON
-
wrote on 4 Dec 2010, 18:19 last edited by
[quote author="luizpaulo" date="1291470389"]Hey a good Qt/C++ JSON parser is avaliable with a LGPL licence: http://qjson.sourceforge.net/
Might be what you need.[/quote]I saw it when I was working on a simple json parser but decided against it. Depending on the task you can get the same result w/o any additional dependencies - just using the script module.
-
wrote on 21 Jun 2011, 14:54 last edited by
Hello here is my point of view! I tested everything! XMLModel, javascript XML parser, C++ side parser etc...
Do Not use C++ Json parser : Because exchange data between c++ and QML is slow
Make a Javascript parser ! It's more easy with eval ! Here is an example :
Json data
@
{"index":["all"],"flux":{"all":[{"data":{"title":"boris","icon":"icon.png"}]}}
@QML side : main.qml
@
import QtQuick 1.0
import "parser.js" as JSItem {
id:root
width: 360
height: 640Component.onCompleted: JS.load() ListModel { id:listModel } ListView { id:view anchors.fill:parent model : listModel delegate: Rectangle { width:parent.width height:80 Text { anchors.center:parent text: title } } }
}
@javascript side : parser.js
@
function load() {listModel.clear(); var xhr = new XMLHttpRequest();
xhr.open("GET","http://data.json",true);
xhr.onreadystatechange = function()
{
if ( xhr.readyState == xhr.DONE)
{
if ( xhr.status == 200)
{
var jsonObject = eval('(' + xhr.responseText + ')');
loaded(jsonObject)
}
}
}
xhr.send();
}function loaded(jsonObject)
{
for ( var index in jsonObject.flux.all )
{
listModel.append({
"title" : jsonObject.flux.all[index].data["title"],
"icon" : jsonObject.flux.all[index].data["icon"]});
}// get directly the json object. Should work but not tested
//listModel.append({jsonObject.flux.all});}
@ -
wrote on 21 Jun 2011, 15:15 last edited by
It is a little surprising to me that you found that exchanging data between C++ and QML to be slow.
I can imagine that maybe the C++ xml parser you tried is slow but I can't imagine creating the model in c++ can be any slower than creating it using Javascript.
Another scenario I can imagine it would slow things is if you are adding elements to a model that is already in use.
I am just curious of the why...
-
wrote on 21 Jun 2011, 15:30 last edited by
That what I m thinking before! But a a guy said me that, and I trust him ... He said me C++ parsing is faster, but exchanging between binary and script is slower.
So, no matter, the javascript method is a little bit more easy than creating a c++ model ! :)
-
wrote on 26 Aug 2011, 10:28 last edited by
I am also not so happy with the performances of my C++ model but I can't precisely say what's slowing down.
-
wrote on 26 Aug 2011, 11:19 last edited by
Hi to all,
I see that the interest in json parsing is always present. I tried to manage qjson but it seems too complex for what can be done and I have developed a method that I am experiencing very efficient and reliable. It concerns of a C++ class and a very short function in js to be used by QML. If someone is interested I can explain it in details in a wiki page. For now the method is integrated in a project but it will be one of the new features of Qt-Complex 2.0 that I am preparing.
Take a look to http://projects.developer.nokia.com/pricemyhouse.
-
wrote on 29 Nov 2011, 20:18 last edited by
Please don't use eval when parsing json-data, you are actually running the code, so if something nasty is in the reply you get, well...
I've made a small library that plays nice with QML, for making the whole ajax-thingie a bit easier, you can check it out at https://github.com/peppelorum/ajaxmee
-
wrote on 28 Dec 2011, 04:47 last edited by
I think the simplest way to parse and turn json data to C++ model is using QScriptEngine. Please see:
@
QScriptValue json = m_scriptEngine->evaluate("JSON.parse").call(QScriptValue(), QScriptValueList() << jsonString);
QVariantList xxxList = json.toVariant().toList();
or
QVariantMap xxxMap = json.toVariant().toMap();
.....
Then, do parse
.....
@And, in the code, m_scriptEngine is your QScriptEngine, and jsonString is your json data
-
wrote on 24 Apr 2012, 18:26 last edited by
QML "includes" a JSON parser. You can just use, without having to include anything:
@var obj = JSON.parse( myJSONString );@
"See json.org for more details":http://www.json.org/js.html
Of course eval can do the trick as well but it is risky and the JSON parser maybe optimized.
-
wrote on 5 Sept 2012, 01:44 last edited by
For anybody still interested, I've just published a JSONListModel component that works just like XMLListModel, but for JSON data instead. It's all pure QML/Javascript, and it even supports queries via JSONPath (XPath for JSON).
You can read more about it there: "Devnet article":http://qt-project.org/wiki/JSONListModel