Parsing JSON with QML



  • Hi,

    I found the best answer to this question in this post: Parsing Json with QML but it is already five years old and I was wondering if there is a better way with QtQuick 2.0.

    There is one issue in particular. If the JSON response is an array the information in the array is lost. This JSON stream:

    {"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":""}]}
    

    Looks like this in the model:

    {"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}}
    

    How can I add the stream to the list model so that z is not lost?


  • Qt Champions 2016

    There has many methods listed in the article. I am not sure which one you mean, but JSON.parse() do not work?



  • Hi,

    I am using the one dridk mentioned. The parsing works fine and I use

    listModel.append({jsonObject});
    

    to fill my ListView. Everything is in the ListModel besides z.
    If I do:

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

    z is added as an array. Just like in the JSON object just without the []-brackets.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.