# List of tiles in JSON is not a list?

**URL:** <https://discourse.mapeditor.org/t/list-of-tiles-in-json-is-not-a-list/1421>\
**Category:** Questions\
**Created:** [June 4, 2016, 8:59pm UTC](https://discourse.mapeditor.org/t/list-of-tiles-in-json-is-not-a-list/1421 "2016-06-04T20:59:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![linck5](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@linck5](https://discourse.mapeditor.org/u/linck5)\
**Post date:** [June 4, 2016, 8:59pm UTC](https://discourse.mapeditor.org/t/list-of-tiles-in-json-is-not-a-list/1421/1 "2016-06-04T20:59:07Z")

</div>

I’m working with unity, and I’m trying to read a json file exported from tiled. So everything was fine until I came across the “tiles” within a tileset.

So the json looks like this:

```
{
    ...,
    "tilesets":[
        {
            ...,
            tiles: 
            {
                "0": ...,
                "1": ...,
                ...
            }
        },
        ...
    ],
    ...
}

```

As you can see, the property “tiles” is not a list of tiles, it is instead an object with some properties inside, like “0”, “1”, etc. I was expecting it to be something like:

```
"tiles": [
    {
        "id": 0,
        ...
    },
    {
        "id": 1,
        ...
    },
    ...
]

```

Is that intended? How am I gonna be able to parse that? I am using the built-in JsonUtility class in unity.

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [June 5, 2016, 8:10am UTC](https://discourse.mapeditor.org/t/list-of-tiles-in-json-is-not-a-list/1421/2 "2016-06-05T08:10:58Z")

</div>

> [@linck5](#):
>
> Is that intended? How am I gonna be able to parse that? I am using the built-in JsonUtility class in unity.

Tile indices are not guaranteed to be consecutive, so even if it was a list, you’d have to iterate it to find each tile by its ID. However, a list may still have been better because the tile IDs have to be converted to strings the way they are stored now, since JSON only allows strings as keys. Of course, changing it now would break everyone’s JSON map reader, so it’s something to reconsider later.

While loading this file, you’ll get the `tiles` property as JSON object. You could iterate all its key/value pairs and write it to some more optimal data structure, converting the keys to integer tile IDs. That is what Tiled does when it loads a JSON tileset.

---

<div class="post-metadata">

**Author:** ![linck5](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@linck5](https://discourse.mapeditor.org/u/linck5)\
**Post date:** [June 5, 2016, 2:54pm UTC](https://discourse.mapeditor.org/t/list-of-tiles-in-json-is-not-a-list/1421/3 "2016-06-05T14:54:22Z")

</div>

Thanks for the answer! How do I iterate through key/value pairs of that object?

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [June 5, 2016, 3:17pm UTC](https://discourse.mapeditor.org/t/list-of-tiles-in-json-is-not-a-list/1421/4 "2016-06-05T15:17:05Z")

</div>

> [@linck5](#):
>
> How do I iterate through key/value pairs of that object?

That depends entirely on the JSON API you’re using.

---

<div class="post-metadata">

**Author:** ![linck5](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@linck5](https://discourse.mapeditor.org/u/linck5)\
**Post date:** [June 5, 2016, 3:24pm UTC](https://discourse.mapeditor.org/t/list-of-tiles-in-json-is-not-a-list/1421/5 "2016-06-05T15:24:29Z")

</div>

Oh, I’m using the unity’s built-in json api that is very simple and probably doesn’t have this feature:  
[http://docs.unity3d.com/ScriptReference/JsonUtility.html](http://docs.unity3d.com/ScriptReference/JsonUtility.html)  
What json api could I use then? Or is there another way to do this?

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [June 5, 2016, 3:35pm UTC](https://discourse.mapeditor.org/t/list-of-tiles-in-json-is-not-a-list/1421/6 "2016-06-05T15:35:14Z")

</div>

Hmm, right, that JSON API looks like a rather high-level convenience thing and you will need to look at a lower-level JSON reader. Maybe try [Json.NET](http://www.newtonsoft.com/json).

---

<div class="post-metadata">

**Author:** ![linck5](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@linck5](https://discourse.mapeditor.org/u/linck5)\
**Post date:** [June 5, 2016, 8:06pm UTC](https://discourse.mapeditor.org/t/list-of-tiles-in-json-is-not-a-list/1421/7 "2016-06-05T20:06:26Z")

</div>

Thanks. I managed to make it work using [Json.NET](http://Json.NET).

Actually [Json.NET](http://Json.NET) doesn’t work in unity, so I used a branch of [Json.NET](http://Json.NET) modified to work with unity, following this tutorial:  
[http://www.what-could-possibly-go-wrong.com/unity-and-nuget/](http://www.what-could-possibly-go-wrong.com/unity-and-nuget/)

After installing that, in my target class, I treated this tiles as a dictionary, like this:

`public Dictionary<string, TileData> tiles;`

And now it magically works.

The only problem is that they say this [unity.Json.NET](http://unity.Json.NET) doesn’t work with iOS, but I think I’m gonna have to deal with that in the future if I ever want to make a build for iOS =(
