json file:
{
"things": [
{
"name": "car",
"speed": 40
},
{
"name": "knife",
"sharpness": 8
}
]
}
c# file:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class TestJson : MonoBehaviour
{
public TextAsset testJson;
void Start()
{
List<Thing> things = JsonUtility.FromJson<Things>(testJson.text).things.ToList();
foreach (Thing thing in things)
{
Debug.Log(thing.name + ", " + thing.speed + ", " + thing.sharpness);
}
}
}
[System.Serializable]
public class Thing
{
public string name;
public float? speed;
public float? sharpness;
}
[System.Serializable]
public class Things
{
public Thing[] things;
}
As you can see I have made the floats, speed and sharpness, nullable, as I want their values to be null if a thing doesn't have that field. However, the output in the log is: