Leviathan
Leviathan

Reputation: 83

JSONPath / QML - Get Elements in String Arrays

So I'm using a JSONListModel for QML, and I need help parsing this JSON File

It's a Minecraft Server Query which gets Online Players' names. However, I'm having trouble adding each node to my List delegate.

This is my code:

ListView {
    JSON.JSONListModel {
        source: "http://minecraft-api.com/v1/players/?server=darkcrest.cf.gs"
        query: "$['players'][*]"
        id: playerNames
    }

    anchors.top: players.bottom
    anchors.topMargin: units.gu(1)

    model: playerNames.model

    delegate: Item {
        Text {
            text: model
        }
    }
}

So as you can see, I have a query, which is the JSONPath to the element I want to view.

What I want to get is the players' names, one after the other. I used a wildcard to get all of the names, but when I set the Text's text to model ("$['players'][0-x]") I get:

file:///home/brendan/UTouch-Apps/ServerStatus/JSONListModel/JSONListModel.qml:15:32: QML ListModel: append: value is not an object

Which points to the line:

property ListModel model : ListModel { id: jsonModel }

In JSONListModel.qml.

So yeah, I'm guessing my path is wrong. I don't know how to handle arrays such as the ones in the JSON file in the link.

If the file were something like:

{
    players: [
         "name": "Whatever1234",
         "name": "Person987"
    ]
}

I could just do

text: model.name

But I can't because the nodes don't have a name. They are just values.

Can someone help me?

Upvotes: 2

Views: 2286

Answers (1)

Matthew
Matthew

Reputation: 2829

Actually from looking at the JSONListModel, ListModel::append, along with the JSON you're parsing the issue is with the append as reported which is at line 40 in JSONListModel.qml

jsonModel.append( jo );

In this case jsonModel is a ListModel therefore append requires jo to be JSON dictionary, i.e.

myListModel.append( {"cost": 5.95, "name":"Pizza"} )

Your query is valid and does return the array the problem is the inner elements are not in the valid form for a ListModel.

There are two options:

  1. Request/Change the source JSON data to conform to name/value encoding or,
  2. Request/Fix JSONListModel to provide the value as an anonymous name or string.

All that said, for #2 it looks like this has already been reported in JSONListModel and is currently unresolved.

Hopefully that answers your question and at least provides you with some options going forward.

Upvotes: 2

Related Questions