Thanks. I saw that it didn't work for me, so I hacked it until it did. 

Below is the end result that actually works:
------------
importQtQuick2.3
importQtQml.Models2.1
import "jsonpath.js"as JSONPath
 
ListModel{
        property string source: ""
        property string json: ""
        property var object:[]
        property string query: ""
 
        onSourceChanged:{
 var xhr= new XMLHttpRequest;
 xhr.open("GET", source);
 xhr.onreadystatechange= function(){
 if( xhr.readyState== XMLHttpRequest.DONE)
 json= JSON.parse(xhr.responseText);
}
 xhr.send();
        }
 
        onJsonChanged:{
 object= JSON.parse(json);
 updateJSONModel(true)
        }
 
        onObjectChanged: updateJSONModel()
        onQueryChanged: updateJSONModel()
 
        function updateJSONModel(){
 clear();
 
 var objectArray= objectArray= JSONPath.jsonPath(object, query);
 for( var key in objectArray){
 var jo= objectArray[key];
 append( jo);
 console.debug("JSONModel: append", JSON.stringify(jo))
}
        }
}

 
--------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Sent: Wednesday, March 25, 2015 at 4:45 PM
From: "Daniel França" <daniel.fra...@gmail.com>
To: "Jason H" <jh...@gmx.com>, "interest@qt-project.org" 
<interest@qt-project.org>
Subject: Re: [Interest] QML ListModels and JSON

I used the JSONListModel for a project, the code is available under MIT license 
here: https://github.com/kromain/qml-utils  
Em qua, 25 de mar de 2015 às 20:58, Jason H <jh...@gmx.com[jh...@gmx.com]> 
escreveu:I get back from a Ajax a JSON object with a 'table' from a web query:
[
{id: 1, "category":"1", value:"1"},
{id: 2, "category":"1", value:"2"},
{id: 3, "category":"2", value:"3"},
...
]

What is the easiest way to convert the array to ListElements?
Up to this point, I was copying data using ListModel.append() but I was hoping 
there was an implicit conversion:

listModel = rows; // where rows = [... the array above... ]

I was also looking for a way to create a model of the distinct values of 
category. I realize there are sections within a ListView, but I want an actual 
model.


THanks.
_______________________________________________
Interest mailing list
Interest@qt-project.org[Interest@qt-project.org]
http://lists.qt-project.org/mailman/listinfo/interest
_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to