Skip to main content
added 287 characters in body
Source Link
Basic
  • 1.3k
  • 10
  • 27
[
    {
        "name": "Sol",
        "type": "star",
        "metals": 0,
        "gas": 0,
        "locations": [
            {
                "name": "Mars",
                "type": "mining",
                "metals": 500,
                "gas": 0
            },
            {
                "name": "Jupiter",
                "type": "gas",
                "metals": 0,
                "gas": 1000,
                "locations": [
                    {
                        "name": "Europa",
                        "type": "moon",
                        "metals": 0,
                        "gas": 0
                    },
                }
            }
        ]
    },
    {
        "name": "Proxima Centauri",
        "type": "star",
        "metals": 0,
        "gas": 0,
        "locations": [
            {
                "name": "Proxima Centauri C",
                "type": "mining",
                "metals": 500,
                "gas": 0
            }
        ]
    }
]

(Check Check out the other parameters on the JsonProperty attribute to get an idea of what's possible).w

[
    {
        "name": "Sol",
        "type": "star",
        "metals": 0,
        "gas": 0,
        "locations": [
            {
                "name": "Mars",
                "type": "mining",
                "metals": 500,
                "gas": 0
            },
            {
                "name": "Jupiter",
                "type": "gas",
                "metals": 0,
                "gas": 1000
            }
        ]
    },
    {
        "name": "Proxima Centauri",
        "type": "star",
        "metals": 0,
        "gas": 0,
        "locations": [
            {
                "name": "Proxima Centauri C",
                "type": "mining",
                "metals": 500,
                "gas": 0
            }
        ]
    }
]

(Check out the other parameters on the JsonProperty to get an idea of what's possible).w

[
    {
        "name": "Sol",
        "type": "star",
        "metals": 0,
        "gas": 0,
        "locations": [
            {
                "name": "Mars",
                "type": "mining",
                "metals": 500,
                "gas": 0
            },
            {
                "name": "Jupiter",
                "type": "gas",
                "metals": 0,
                "gas": 1000,
                "locations": [
                    {
                        "name": "Europa",
                        "type": "moon",
                        "metals": 0,
                        "gas": 0
                    },
                }
            }
        ]
    },
    {
        "name": "Proxima Centauri",
        "type": "star",
        "metals": 0,
        "gas": 0,
        "locations": [
            {
                "name": "Proxima Centauri C",
                "type": "mining",
                "metals": 500,
                "gas": 0
            }
        ]
    }
]

Check out the other parameters on the JsonProperty attribute to get an idea of what's possible.

added 49 characters in body
Source Link
Basic
  • 1.3k
  • 10
  • 27
using Newtonsoft.Json;
...
var rootLocationList = JsonConvert.DeserializeObject<List<Location>>(jsonConfig);

rootLocationList is now a List<Location>

If a given location has childrenchild locations, there will be a populated list at locations, otherwise locations will be null.

YouAny location can have sub-locations, so you have almost¹ as many levels of nesting as you wish... Any location can have sub-locations.

Note that I've changed the root of your jsonJson to a list of locations. If you want other properties besides a list of locations, create a root object with a List<Location> locations property and whatever other properties you need).

var rootLocationList = JsonConvert.DeserializeObject<List<Location>>(jsonConfig);

If a location has children, there will be a populated list at locations, otherwise locations will be null.

You can have almost¹ as many levels of nesting as you wish... Any location can have sub-locations.

Note that I've changed the root of your json to a list of locations. If you want other properties besides a list of locations, create a root object with a List<Location> locations property and whatever other properties you need).

using Newtonsoft.Json;
...
var rootLocationList = JsonConvert.DeserializeObject<List<Location>>(jsonConfig);

rootLocationList is now a List<Location>

If a given location has child locations, there will be a populated list at locations, otherwise locations will be null.

Any location can have sub-locations, so you have almost¹ as many levels of nesting as you wish.

Note that I've changed the root of your Json to a list of locations. If you want other properties besides a list of locations, create a root object with a List<Location> locations property and whatever other properties you need).

Source Link
Basic
  • 1.3k
  • 10
  • 27

As @Evorlor mentioned in comments, you should using Newtonsoft Json (also known as Json.Net)

https://docs.unity3d.com/Packages/[email protected]/manual/index.html

It's the go-to default everywhere C# is used, it's cross-platform, very flexible and incredibly lightweight.

Once you've got that installed, all you need to do is define an object that has the same structure as your Json.

So... If you hypothetically wanted to support this Json:

[
    {
        "name": "Sol",
        "type": "star",
        "metals": 0,
        "gas": 0,
        "locations": [
            {
                "name": "Mars",
                "type": "mining",
                "metals": 500,
                "gas": 0
            },
            {
                "name": "Jupiter",
                "type": "gas",
                "metals": 0,
                "gas": 1000
            }
        ]
    },
    {
        "name": "Proxima Centauri",
        "type": "star",
        "metals": 0,
        "gas": 0,
        "locations": [
            {
                "name": "Proxima Centauri C",
                "type": "mining",
                "metals": 500,
                "gas": 0
            }
        ]
    }
]

You could use a class structure like this:

public class Location
{
    public string name { get; set; }
    public string type { get; set; }
    public int metals { get; set; }
    public int gas { get; set; }

    public List<Location> locations { get; set; }
}

And if your json is in a string jsonConfig...

var rootLocationList = JsonConvert.DeserializeObject<List<Location>>(jsonConfig);

If a location has children, there will be a populated list at locations, otherwise locations will be null.

You can have almost¹ as many levels of nesting as you wish... Any location can have sub-locations.

Note that I've changed the root of your json to a list of locations. If you want other properties besides a list of locations, create a root object with a List<Location> locations property and whatever other properties you need).

You can also map using different names in the Json, should you wish:

public class Location
{
    ...
    [JsonProperty("locations")]
    public List<Location> childLocations { get; set; }
}

(Check out the other parameters on the JsonProperty to get an idea of what's possible).w

¹ There's a default limit of 64 levels deep, but it's configurable.