- Home /
Question by
davidneudecker · Aug 23, 2019 at 05:30 AM ·
json
How to define by user which field name to get from JSON in Unity with JsonUtility?
I am able to read JSON in Unity when I specify field name by variables names in my class with JsonUtility. I want to make my script more generic so user can define which field name to use from multiple JSONs, which don't necessary use same field names.
// call this function to get array of JsonEntry objects
public static T[] FromJson<T>(string json)
{
return JsonUtility.FromJson<JsonEntries<T>>(json).Entries;
}
I am using this classes
[Serializable]
public class JsonEntries<T>
{
public T[] Entries;
}
[Serializable]
public class JsonEntry
{
// field names from one JSON example
public string USULAN_ID;
public string USULAN_LAT;
public string USULAN_LONG;
public string USULAN_URGENSI;
//TODO user defined filed names
}
Is it possible to let user define which field name to use, if I don't know the field names in advance with using JsonUtility in Unity?
Comment
Your answer
