Repository navigation
Expand file tree
/
Copy pathJSONListModel.qml
More file actions
93 lines (81 loc) · 2.64 KB
/
Copy pathJSONListModel.qml
File metadata and controls
93 lines (81 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* JSONListModel - a QML ListModel with JSON and JSONPath support
*
* Copyright (c) 2012 Romain Pokrzywka (KDAB) (romain@kdab.com)
* Licensed under the MIT licence (http://opensource.org/licenses/mit-license.php)
*/
import QtQuick 2.0
import "jsonpath.js" as JSONPath
Item {
property string source: ""
property string contentType: ""
property string postData: ""
property string requestType: "GET"
property string json: ""
property string query: ""
property bool logging: false
property bool loading: false
property bool listModelUpdate: false
property ListModel model : ListModel { id: jsonModel }
property alias count: jsonModel.count
Component.onCompleted: {
if (source !== "")
refresh()
else if (json !== "")
updateJSONModel()
}
function refresh() {
loading = true;
var xhr = new XMLHttpRequest;
if (logging) {
console.log("Sending request to JSON API")
console.log("Request Type: "+requestType);
console.log("Source: "+source);
}
xhr.open(requestType, source);
if (contentType !== "")
xhr.setRequestHeader("Content-Type", contentType);
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (logging) {
console.log("status: " + xhr.status + ", " + xhr.statusText)
console.log("json: " + xhr.responseText);
console.log("response headers: " + xhr.getAllResponseHeaders());
}
json = xhr.responseText;
updateJSONModel();
}
}
if (logging) console.log("Post Data: "+postData)
if (requestType == "POST") {
xhr.send(postData);
} else {
xhr.send()
}
}
function updateJSONModel() {
if (jsonModel.count > 0) {
jsonModel.clear();
}
if ( json === "") {
loading = false;
return;
}
if (logging) console.log(json);
var objectArray = parseJSONString(json, query);
for ( var key in objectArray ) {
var jo = objectArray[key];
jsonModel.append(jo);
}
loading = false;
listModelUpdate = !listModelUpdate;
}
function parseJSONString(jsonString, jsonPathQuery) {
var objectArray = JSON.parse(jsonString);
if ( jsonPathQuery !== "" )
objectArray = JSONPath.jsonPath(objectArray, jsonPathQuery);
return objectArray;
}
function clear() {
jsonModel.clear();
}
}