Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How do I load json array?

Discussion in 'Scripting' started by Mohammed28_, Feb 24, 2023.

  1. Mohammed28_

    Mohammed28_

    Joined:
    Apr 23, 2022
    Posts:
    11
    This is a portion of the json file I use, the json file I am using is a dataset containing all countries borders, I obtained the dataset from public.opendatasoft.com.

    Everything works fine when i load the json file except fot the coordinates, which always returns null for some unidentified reason.

    The c# class I use to load the data to:
    Code (CSharp):
    1.    [System.Serializable]
    2.     public class Geometry
    3.     {
    4.         public List<List<List<List<double>>>> coordinates;
    5.         public string type;
    6.     }
    7.  
    8.     [System.Serializable]
    9.     public class GeoPoint2d
    10.     {
    11.         public double lon;
    12.         public double lat;
    13.     }
    14.  
    15.     [System.Serializable]
    16.     public class GeoShape
    17.     {
    18.         public string type;
    19.         public Geometry geometry;
    20.         public Properties properties;
    21.     }
    22.  
    23.     [System.Serializable]
    24.     public class Properties
    25.     {
    26.     }
    27.  
    28.     [System.Serializable]
    29.     public class Border
    30.     {
    31.         public GeoPoint2d geo_point_2d;
    32.         public GeoShape geo_shape;
    33.         public string iso3;
    34.         public string status;
    35.         public string color_code;
    36.         public string name;
    37.         public string continent;
    38.         public string region;
    39.         public string iso_3166_1_alpha_2_codes;
    40.         public string french_short;
    41.     }
    42.  
    43.     [System.Serializable]
    44.     public class BordersRoot
    45.     {
    46.         public Border[] borders;
    47.     }
    48.  
    49.  
    I cant think of a reason for why this isn't working
     
    Last edited: Feb 24, 2023
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    This isn't valid json.

    Edit: Further...
    List<List<List<List<double>>>>
    wth is this? How mangled is your json that you need 4 nested lists?
     
  3. Mohammed28_

    Mohammed28_

    Joined:
    Apr 23, 2022
    Posts:
    11
    The json is valid, The eroor is from the forums, check it out here https://jpst.it/37tkx
     
  4. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    "coordinates""type"
    is definitely NOT valid json. The link you've posted is.

    Your root-object only contains 1 object. Thus, BordersRoot should look like:
    Code (CSharp):
    1. [System.Serializable]
    2. public class BordersRoot
    3. {
    4.     public Border border;
    5. }
    For me, Unity's JsonUtility seems to really hate this json. It just gives me an empty object. (using
    JsonUtility.FromJsonOverwrite(txt.text, root);
    )
    The Newtonsoft Json-Package (com.unity.nuget.newtonsoft-json) deserializes it without any issues, including all of the coordinates. (using
    JsonConvert.PopulateObject(txt.text, root);
    )
     
  5. Mohammed28_

    Mohammed28_

    Joined:
    Apr 23, 2022
    Posts:
    11
    you do not understand, I have put one object as example but the actual json file has 250+ countries. You can find the full json file here.
    Also the error is in the coordinates field only, other fields loas normally.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    So the classes here do look strange as others have mentioned, but you didn't actually show how you're loading the Json into this data structure. Are you using JsonUtility? If so, its biggest caveat is that it doesn't support arrays. Newtonsoft's Json.NET is generally the recommended solution if you need anything beyond the most basic of JSON functionality.
     
  7. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Removing one list parses one of the objects properly.
    Going to the full doc throws:

    So some of them are
    List<List<List<List<double>>>>
    whereas others are
    List<List<List<double>>>
    .
     
  8. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    This one is three layers deep.

    Code (CSharp):
    1. [{"geo_point_2d":{"lon":33.74379108021756,"lat":21.892740179494872},"geo_shape":{"type":"Feature","geometry":{"coordinates":[[[33.251040000000046,21.99977000000007],[34.15064000000007,21.996030000000076],[34.05218000000008,21.784320000000037],[33.600490000000036,21.744060000000047],[33.251040000000046,21.99977000000007]]],"type":"Polygon"},"properties":{}},"iso3":null,"status":"Adm. by EGY","color_code":"EGY","name":"Ma'tan al-Sarra","continent":"Africa","region":"Northern Africa","iso_3166_1_alpha_2_codes":null,"french_short":"Ma'tan al-Sarra"}]

    This one is 4 layers deep:

    Code (CSharp):
    1. {"geo_point_2d":{"lon":23.89812325011703,"lat":55.3356704855903},"geo_shape":{"type":"Feature","geometry":{"coordinates":[[[[20,55],[26.613210000000038,55.67483000000004]]]],"type":"MultiPolygon"},"properties":{}},"iso3":"LTU","status":"Member State","color_code":"LTU","name":"Lithuania","continent":"Europe","region":"Northern Europe","iso_3166_1_alpha_2_codes":"LT","french_short":"Lituanie"}
    Edit: Ah, so that's where the 'invalid json' came from.. The forum will simply not post the actual json here if it's too long.. Removed all but first & last coord from 2nd Code-block.



    In short: Looks like some of them have multiple 'coordinate sets'. They added an additional layer of nesting for those.
     
  9. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    So. Looking at them again:

    type: Polygon is 3 layers deep.
    type: MultiPolygon is 4 layers deep. It adds 1 additional layer of nesting for each polygon.
     
  10. Mohammed28_

    Mohammed28_

    Joined:
    Apr 23, 2022
    Posts:
    11
    Yeah, I just noticed that and came to check the forum again, I think I will need to use something like dynamic.
     
  11. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    That's why I often times don't use object mappers at all ^^. While it can be useful in some cases to map json data to an actual predefined object structure, sometimes it's enough to just load the data in an abstract way. I've written my SimpleJSON parser several years ago exactly for that purpose. It's essentially just a single file, but it has some extension files which may be useful. Like the Unity specific SimpleJSONUnity.cs which adds support for Vector2/3/4 and some other types. It supports objects which members x, y, z, w or arrays with 2,3 or 4 elements. My parser just parses the data into json primitive types (Objects, Arrays, Number, String, Boolean, Null) and provides convenient conversion properties and implilcit type conversion. Though it's quite easy to extend to add your own conversion logic.

    Of course Newtonsoft's Json.NET provides similar functionality. Though my parser is way smaller and easy to extend, Every class is partial and almost every method is virtual. When you're dealing with more complex structures it's often times easier to just keep the general structure as it is and just extract the parts you really care about into objects with a custom conversion.