Create multiple structs from JSON file

Hello, I am trying to figure out the best way to create a list of structs from one JSON file. The JSON file has data organized as such:

{
“FIELD1”: “ORIGIN”,
“FIELD2”: “874”,
“FIELD3”: “37.7848”,
“FIELD4”: “-122.7278”,
“FIELD5”: “San Francisco Bay, CA”,
“FIELD6”: “United States”
}
{
“FIELD1”: “ORIGIN”,
“FIELD2”: “36”,
“FIELD3”: “37.5242”,
“FIELD4”: “-77.4932”,
“FIELD5”: “Richmond, VA”,
“FIELD6”: “United States”
}
etc…

My code currently looks like this

public class ParseJSON : MonoBehaviour {

    public struct Attendee {
        public string FIELD1;
        public string FIELD2;
        public string FIELD3;
        public string FIELD4;
        public string FIELD5;
        public string FIELD6;
    }
 
    List<Attendee> attendees = new List<Attendee>();

    void Start(){
         ReadData();
    }

    void ReadData(){
        Attendee = new Attendee();
        //Trying to figure out how to read through my JSON data
        //and populate here
        attendees.add(attendee);
    }
}

I have looked in the Unity documentation and I have used the example to read JSON to a Struct when the JSON file only contains one entry, I am just trying to figure out how to handle multiple entries in one JSON file. Thanks in advanced for any help that you may be able to provide!

If that’s the exact JSON, it’s invalid, it needs a comma between objects, and the objects need to be in a list. So it would look like:

[
{
"FIELD1": "ORIGIN",
"FIELD2": "874",
"FIELD3": "37.7848",
"FIELD4": "-122.7278",
"FIELD5": "San Francisco Bay, CA",
"FIELD6": "United States"
},
{
"FIELD1": "ORIGIN",
"FIELD2": "36",
"FIELD3": "37.5242",
"FIELD4": "-77.4932",
"FIELD5": "Richmond, VA",
"FIELD6": "United States"
}
]

JsonUtility doesn’t support having a list as the top level object, but that’s fairly easy to work around. See How to load an array with JsonUtility? - Unity Engine - Unity Discussions

But essentially you wouldn’t read one object at a time, you’d read the entire file in one call and a List (or a class containing such a list) would be the result.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class ParseJSON : MonoBehaviour {

    string jsonString;
    public TextAsset jsonTextAsset;

    [System.Serializable]
    public struct Attendee {
        public string FIELD1;
        public string FIELD2;
        public string FIELD3;
        public string FIELD4;
        public string FIELD5;
        public string FIELD6;
        public string FIELD7;
        public string FIELD8;
        public string FIELD9;
        public string FIELD10;
        public string FIELD11;
        public string FIELD12;
        public string FIELD13;
        public string FIELD14;
        public string FIELD15;
    }

    [System.Serializable]
    public class AttendeeList {
        public Attendee[] attendees;
    }

    void Start () {
        ReadData ();
    }
      
    private void ReadData(){
        jsonString = jsonTextAsset.text;
        var a = JsonUtility.FromJson<AttendeeList> (jsonString);
    }
}

Thanks for the help, my code currently looks like this, but it is throwing a "ArgumentException: JSON must represent an object type. Any idea why?

Also, my JSON file looks exactly like the one you posted (Except with 15 fields), I had just quickly written an example before of the type of fields that I have. Thanks again!

After doing some more digging around, I found a solution that allows me to easily convert from Json to an array of structs. Thanks again for your help.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class ParseJSON : MonoBehaviour {

    string jsonString;
    public TextAsset jsonTextAsset;

    [System.Serializable]
    public struct Attendee {
        public string FIELD1;
        public string FIELD2;
        public string FIELD3;
        public string FIELD4;
        public string FIELD5;
        public string FIELD6;
        public string FIELD7;
        public string FIELD8;
        public string FIELD9;
        public string FIELD10;
        public string FIELD11;
        public string FIELD12;
        public string FIELD13;
        public string FIELD14;
        public string FIELD15;
    }

    void Start () {
        ReadData ();
    }
       
    private void ReadData(){
        jsonString = jsonTextAsset.text;
        Attendee[] attendees = JsonHelper.getJsonArray<Attendee> (jsonString);
        foreach (Attendee a in attendees) {
            Debug.Log (a.FIELD1);
        }
    }
}

public class JsonHelper {
    public static T[] getJsonArray<T>(string json)
    {
        string newJson = "{ \"array\": " + json + "}";
        Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>> (newJson);
        return wrapper.array;
    }

    [System.Serializable]
    private class Wrapper<T>
    {
        public T[] array;
    }
}
1 Like