在QML中解析JSON [英] Parsing JSON in QML
问题描述
相关的Qt文档应为此.但是它没有提到QML.但是,在网上的许多地方,我都发现了QML JS中的JSON.parse
之类的功能的用法.有这样的功能,我该如何使用?
The relevant Qt doc should be this. But it makes no mention of QML. Yet on many places on the net I find usage of functions like JSON.parse
in QML JS. Is there such a function and how do I use it?
我只想索取文档链接,但此处被认为是题外话.
I'd just ask for a link to documentation but that's considered off-topic here.
推荐答案
在QML中解析JSON与解析链接),特别是针对QML进行了一些修改.
Parsing JSON in QML is no different than parsing JSON in Javascript, because QML provides an environment based on ECMAScript (link) with some modifications especially for QML.
因此您可以使用内置的JSON.parse()
函数.以下示例在QML中是可能的:
So you can use the in-built 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);
}
}
这就是为什么Qt文档没有说明有关此特定功能的任何原因:
And this is the reason why the Qt docs don't state anything about this particular function:
QML文档中未明确记录标准的ECMAScript内置文件.有关其用法的更多信息,请参考ECMA-262第5版标准或许多在线JavaScript参考和教程网站之一,例如W3Schools JavaScript参考("JavaScript对象参考"部分)
The standard ECMAScript built-ins are not explicitly documented in the QML documentation. For more information on their use, please refer to the ECMA-262 5th edition standard or one of the many online JavaScript reference and tutorial sites, such as the W3Schools JavaScript Reference (JavaScript Objects Reference section)
这篇关于在QML中解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!