jsonqmllistmodel

qml append json stream to listmodel


I would like to append an answer in JSON from a web server to a QML ListModel. Currently I am using

eventModel.append(jsonObject)

which works fine if the answer only contains strings or numbers but not if there is an array within the answer. I am using the code from here to get the JSON object.

This is one line of the answer:

{"i":3814086,"t":"d","s":1479970800,"sw":"Do","sds":"24.11.16","ss":"08:00","e":1479996000,"eds":"24.11.16","es":"15:00","f":false,"z":[{"i":223500,"d":true,"r":"","h":null,"hs":null,"hss":"","he":null,"hes":""}]}

Everything is added fine beside z. If I read the entries from the list model I get this:

{"objectName":"","i":3814086,"t":"d","s":1479970800,"sw":"Do","sds":"24.11.16","ss":"08:00","e":1479996000,"eds":"24.11.16","es":"15:00","f":false,"z":{"objectName":"","count":1,"dynamicRoles":false}}

It looks like everything in z is lost. I already tried to add it again

for(var i in jsonObject){
    eventModel.append(jsonObject[i])
    eventModel.set(i, {"z":jsonObject[i]["z"]})
}

but the result is the same.

Is something like this just not possible or am I doing something wrong here when appending the JSON object to the list model?


Solution

  • This is taken from one of my apps and it can be an illustrative example for your case.

    ListModel {
            id:agenciesModel
    
            ListElement {
                name: "401"
                eventListDates :[
                    ListElement{
                        date:"jj/mm/aaaa"
                    }
    
                ]
            }
    
            ListElement {
                name: "402"
                eventListDates :[
                    ListElement{
                        date:"jj/mm/aaaa"
                    }
    
                ]
    
    
            }
    
            ListElement {
                name: "403"  
                eventListDates :[
                    ListElement{
                        date:"jj/mm/aaaa"
                    }
    
                ]
    
    
    
            }
    }
    var listObjJS = [{"date":"10/10/2019"},
                     {"date":"10/11/2011"},
                     {"date":"10/11/2011"},
                     {"date":"10/11/2011"}
                    ];
    
    for(var j=0;j<listObjJS.length;j++ )
       agenciesModel.get(i).eventListDates.append(
            { date : listObjJS[j].toString().split(":")[1]}
       );