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