I need to get at the data that inside of “hits”, and I’m not sure on how I can do that with the JSON utility, and how I need to structure my JSON object classes in C#.
Do I need an external JSON library for this? Any help would be appreciated.
The Unity jsonutility deserializes to a defined type in your program. So you’ll need to define a class (or set of classes if the objects nest) that mirror the shape of the json.
something like this:
public class Data
{
public int took;
public bool timed_out;
public ShardsData _shards;
public HitsData hits;
}
public class ShardsData
{
public int total;
public int successful;
public int failed;
}
public class HitsData
{
public int total;
public float max_score;
public HitsAuxData[] hits;
}
public class HitsAuxData
{
public string _index;
public string _type;
}
If you don’t want to have to do that… you can use a 3rd party library instead that allows traversing json generically.
I usually use the site json2csharp to convert a json into classes. It provides properties, but I believe unity’s json uses fields instead and must be serialized. You normally deserialize your json string into a class and then you can access your data through the variables like normal. I’m not sure if Unity’s json lets you access the fields through the string by parsing it.