QML and JSON
-
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