Parsing JSON in QML

The corresponding Qt document should be this . But he does not mention QML. However, in many places on the network I find the use of features such as JSON.parse in QML JS. Is there such a function and how to use it?

I would just ask for a link to the documentation, but this was considered off topic.

+5
source share
1 answer

Parsing JSON in QML is no different from parsing JSON in Javascript , because QML provides an ECMAScript-based environment ( link ) with some changes, especially for QML.

So you can use the built-in JSON.parse() function. The following example is possible in QML:

 import QtQuick 2.7 import QtQuick.Window 2.2 Window { visible: true width: 640 height: 480 title: qsTr("Hello World") Component.onCompleted: { var JsonString = '{"a":"A whatever, run","b":"B fore something happens"}'; var JsonObject= JSON.parse(JsonString); //retrieve values from JSON again var aString = JsonObject.a; var bString = JsonObject.b; console.log(aString); console.log(bString); } } 

And for that very reason, the Qt docs don't say anything about this particular function:

The standard ECMAScript built-in modules are not explicitly documented in the QML documentation. For more information about their use, please refer to the ECMA-262 5th edition standard or one of the many interactive JavaScript links and tutorials, such as the W3Schools JavaScript link (link to JavaScript objects)

A source

+11
source

Source: https://habr.com/ru/post/1263462/


All Articles