2016-09-15 07:07:13 -04:00
|
|
|
/* 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)
|
|
|
|
*/
|
|
|
|
|
2017-04-16 08:46:43 -04:00
|
|
|
import QtQuick 2.7
|
2016-09-15 07:07:13 -04:00
|
|
|
import "jsonpath.js" as JSONPath
|
|
|
|
|
|
|
|
Item {
|
|
|
|
property string source: ""
|
|
|
|
property string json: ""
|
|
|
|
property string query: ""
|
|
|
|
|
|
|
|
property ListModel model : ListModel { id: jsonModel }
|
|
|
|
property alias count: jsonModel.count
|
|
|
|
|
|
|
|
onSourceChanged: {
|
|
|
|
var xhr = new XMLHttpRequest;
|
|
|
|
xhr.open("GET", source);
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
if (xhr.readyState == XMLHttpRequest.DONE)
|
|
|
|
json = xhr.responseText;
|
|
|
|
}
|
|
|
|
xhr.send();
|
|
|
|
}
|
|
|
|
|
|
|
|
onJsonChanged: updateJSONModel()
|
|
|
|
onQueryChanged: updateJSONModel()
|
|
|
|
|
|
|
|
function updateJSONModel() {
|
|
|
|
jsonModel.clear();
|
|
|
|
|
|
|
|
if ( json === "" )
|
|
|
|
return;
|
|
|
|
|
|
|
|
var objectArray = parseJSONString(json, query);
|
|
|
|
for ( var key in objectArray ) {
|
|
|
|
var jo = objectArray[key];
|
|
|
|
jsonModel.append( jo );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseJSONString(jsonString, jsonPathQuery) {
|
|
|
|
var objectArray = JSON.parse(jsonString);
|
|
|
|
if ( jsonPathQuery !== "" )
|
|
|
|
objectArray = JSONPath.jsonPath(objectArray, jsonPathQuery);
|
|
|
|
|
|
|
|
return objectArray;
|
|
|
|
}
|
|
|
|
}
|