Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Parsing JSON with QML
Qt 6.11 is out! See what's new in the release blog

Parsing JSON with QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
json
3 Posts 2 Posters 5.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • benlauB Offline
      benlauB Offline
      benlau
      Qt Champions 2016
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved