Search Unity

Create multiple structs from JSON file

Discussion in 'Scripting' started by ScottRichards, Aug 23, 2017.

  1. ScottRichards

    ScottRichards

    Joined:
    Oct 20, 2016
    Posts:
    24
    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

    Code (CSharp):
    1. public class ParseJSON : MonoBehaviour {
    2.  
    3.     public struct Attendee {
    4.         public string FIELD1;
    5.         public string FIELD2;
    6.         public string FIELD3;
    7.         public string FIELD4;
    8.         public string FIELD5;
    9.         public string FIELD6;
    10.     }
    11.  
    12.     List<Attendee> attendees = new List<Attendee>();
    13.  
    14.     void Start(){
    15.          ReadData();
    16.     }
    17.  
    18.     void ReadData(){
    19.         Attendee = new Attendee();
    20.         //Trying to figure out how to read through my JSON data
    21.         //and populate here
    22.         attendees.add(attendee);
    23.     }
    24. }
    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!
     
    Last edited: Aug 23, 2017
  2. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    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:
    Code (csharp):
    1. [
    2. {
    3. "FIELD1": "ORIGIN",
    4. "FIELD2": "874",
    5. "FIELD3": "37.7848",
    6. "FIELD4": "-122.7278",
    7. "FIELD5": "San Francisco Bay, CA",
    8. "FIELD6": "United States"
    9. },
    10. {
    11. "FIELD1": "ORIGIN",
    12. "FIELD2": "36",
    13. "FIELD3": "37.5242",
    14. "FIELD4": "-77.4932",
    15. "FIELD5": "Richmond, VA",
    16. "FIELD6": "United States"
    17. }
    18. ]
    JsonUtility doesn't support having a list as the top level object, but that's fairly easy to work around. See https://forum.unity3d.com/threads/how-to-load-an-array-with-jsonutility.375735/

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

    ScottRichards

    Joined:
    Oct 20, 2016
    Posts:
    24
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5.  
    6. public class ParseJSON : MonoBehaviour {
    7.  
    8.     string jsonString;
    9.     public TextAsset jsonTextAsset;
    10.  
    11.     [System.Serializable]
    12.     public struct Attendee {
    13.         public string FIELD1;
    14.         public string FIELD2;
    15.         public string FIELD3;
    16.         public string FIELD4;
    17.         public string FIELD5;
    18.         public string FIELD6;
    19.         public string FIELD7;
    20.         public string FIELD8;
    21.         public string FIELD9;
    22.         public string FIELD10;
    23.         public string FIELD11;
    24.         public string FIELD12;
    25.         public string FIELD13;
    26.         public string FIELD14;
    27.         public string FIELD15;
    28.     }
    29.  
    30.     [System.Serializable]
    31.     public class AttendeeList {
    32.         public Attendee[] attendees;
    33.     }
    34.  
    35.     void Start () {
    36.         ReadData ();
    37.     }
    38.      
    39.     private void ReadData(){
    40.         jsonString = jsonTextAsset.text;
    41.         var a = JsonUtility.FromJson<AttendeeList> (jsonString);
    42.     }
    43. }
    44.  
    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!
     
  4. ScottRichards

    ScottRichards

    Joined:
    Oct 20, 2016
    Posts:
    24
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5.  
    6. public class ParseJSON : MonoBehaviour {
    7.  
    8.     string jsonString;
    9.     public TextAsset jsonTextAsset;
    10.  
    11.     [System.Serializable]
    12.     public struct Attendee {
    13.         public string FIELD1;
    14.         public string FIELD2;
    15.         public string FIELD3;
    16.         public string FIELD4;
    17.         public string FIELD5;
    18.         public string FIELD6;
    19.         public string FIELD7;
    20.         public string FIELD8;
    21.         public string FIELD9;
    22.         public string FIELD10;
    23.         public string FIELD11;
    24.         public string FIELD12;
    25.         public string FIELD13;
    26.         public string FIELD14;
    27.         public string FIELD15;
    28.     }
    29.  
    30.     void Start () {
    31.         ReadData ();
    32.     }
    33.        
    34.     private void ReadData(){
    35.         jsonString = jsonTextAsset.text;
    36.         Attendee[] attendees = JsonHelper.getJsonArray<Attendee> (jsonString);
    37.         foreach (Attendee a in attendees) {
    38.             Debug.Log (a.FIELD1);
    39.         }
    40.     }
    41. }
    42.  
    43. public class JsonHelper {
    44.     public static T[] getJsonArray<T>(string json)
    45.     {
    46.         string newJson = "{ \"array\": " + json + "}";
    47.         Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>> (newJson);
    48.         return wrapper.array;
    49.     }
    50.  
    51.     [System.Serializable]
    52.     private class Wrapper<T>
    53.     {
    54.         public T[] array;
    55.     }
    56. }
     
    wenpu86 likes this.