Search Unity

Question How to access this json class

Discussion in 'Scripting' started by pKallv, Sep 27, 2022.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    I have used simple json structures with jsonutility but now I have, IMHO, a complex json structure:

    Code (CSharp):
    1. [Serializable]
    2. public class Root
    3. {
    4.     public string type { get; set; }
    5.     public Geometry geometry { get; set; }
    6.     public Properties properties { get; set; }
    7. }
    8.  
    9. // Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
    10. [Serializable]
    11. public class Data
    12. {
    13.     public Instant instant { get; set; }
    14.     public Next12Hours next_12_hours { get; set; }
    15.     public Next1Hours next_1_hours { get; set; }
    16.     public Next6Hours next_6_hours { get; set; }
    17. }
    18.  
    19. [Serializable]
    20. public class Details
    21. {
    22.     public double air_pressure_at_sea_level { get; set; }
    23.     public double air_temperature { get; set; }
    24.     public double cloud_area_fraction { get; set; }
    25.     public double relative_humidity { get; set; }
    26.     public double wind_from_direction { get; set; }
    27.     public double wind_speed { get; set; }
    28.     public double precipitation_amount { get; set; }
    29. }
    30.  
    31. [Serializable]
    32. public class Geometry
    33. {
    34.     public string type { get; set; }
    35.     public List<double> coordinates { get; set; }
    36. }
    37.  
    38. [Serializable]
    39. public class Instant
    40. {
    41.     public Details details { get; set; }
    42. }
    43.  
    44. [Serializable]
    45. public class Meta
    46. {
    47.     public DateTime updated_at { get; set; }
    48.     public Units units { get; set; }
    49. }
    50.  
    51. [Serializable]
    52. public class Next12Hours
    53. {
    54.     public Summary summary { get; set; }
    55. }
    56.  
    57. [Serializable]
    58. public class Next1Hours
    59. {
    60.     public Summary summary { get; set; }
    61.     public Details details { get; set; }
    62. }
    63.  
    64. [Serializable]
    65. public class Next6Hours
    66. {
    67.     public Summary summary { get; set; }
    68.     public Details details { get; set; }
    69. }
    70.  
    71. [Serializable]
    72. public class Properties
    73. {
    74.     public Meta meta { get; set; }
    75.     public List<Timeseries> timeseries { get; set; }
    76. }
    77.  
    78.  
    79. [Serializable]
    80. public class Summary
    81. {
    82.     public string symbol_code { get; set; }
    83. }
    84.  
    85. [Serializable]
    86. public class Timeseries
    87. {
    88.     public DateTime time { get; set; }
    89.     public Data data { get; set; }
    90. }
    91.  
    92. [Serializable]
    93. public class Units
    94. {
    95.     public string air_pressure_at_sea_level { get; set; }
    96.     public string air_temperature { get; set; }
    97.     public string cloud_area_fraction { get; set; }
    98.     public string precipitation_amount { get; set; }
    99.     public string relative_humidity { get; set; }
    100.     public string wind_from_direction { get; set; }
    101.     public string wind_speed { get; set; }
    102. }
    I have added the "[Serializable]".

    I have used the
    Code (CSharp):
    1. json2csharp.com
    website to generate this.

    I used the same site to generate "example":

    Code (CSharp):
    1. Data data = new Data()
    2.         {
    3.             instant = new Instant()
    4.             {
    5.                 details = new Details()
    6.                 {
    7.                     air_pressure_at_sea_level = 1,
    8.                     air_temperature = 1,
    9.                     cloud_area_fraction = 1,
    10.                     relative_humidity = 1,
    11.                     wind_from_direction = 1,
    12.                     wind_speed = 1,
    13.                     precipitation_amount = 1,
    14.                 },
    15.             },
    16.             next_12_hours = new Next12Hours()
    17.             {
    18.                 summary = new Summary()
    19.                 {
    20.                     symbol_code = "",
    21.                 },
    22.             },
    23.             next_1_hours = new Next1Hours()
    24.             {
    25.                 summary = new Summary()
    26.                 {
    27.                     symbol_code = "",
    28.                 },
    29.                 details = new Details()
    30.                 {
    31.                     air_pressure_at_sea_level = 1,
    32.                     air_temperature = 1,
    33.                     cloud_area_fraction = 1,
    34.                     relative_humidity = 1,
    35.                     wind_from_direction = 1,
    36.                     wind_speed = 1,
    37.                     precipitation_amount = 1,
    38.                 },
    39.             },
    40.             next_6_hours = new Next6Hours()
    41.             {
    42.                 summary = new Summary()
    43.                 {
    44.                     symbol_code = "",
    45.                 },
    46.                 details = new Details()
    47.                 {
    48.                     air_pressure_at_sea_level = 1,
    49.                     air_temperature = 1,
    50.                     cloud_area_fraction = 1,
    51.                     relative_humidity = 1,
    52.                     wind_from_direction = 1,
    53.                     wind_speed = 1,
    54.                     precipitation_amount = 1,
    55.                 },
    56.             },
    57.         };
    58.  
    59.         Details details = new Details()
    60.         {
    61.             air_pressure_at_sea_level = 1,
    62.             air_temperature = 1,
    63.             cloud_area_fraction = 1,
    64.             relative_humidity = 1,
    65.             wind_from_direction = 1,
    66.             wind_speed = 1,
    67.             precipitation_amount = 1,
    68.         };
    69.  
    70.         Geometry geometry = new Geometry()
    71.         {
    72.             type = "",
    73.             coordinates = new List<double>(),
    74.         };
    75.  
    76.         Instant instant = new Instant()
    77.         {
    78.             details = new Details()
    79.             {
    80.                 air_pressure_at_sea_level = 1,
    81.                 air_temperature = 1,
    82.                 cloud_area_fraction = 1,
    83.                 relative_humidity = 1,
    84.                 wind_from_direction = 1,
    85.                 wind_speed = 1,
    86.                 precipitation_amount = 1,
    87.             },
    88.         };
    89.  
    90.         Meta meta = new Meta()
    91.         {
    92.             updated_at = DateTime.Now,
    93.             units = new Units()
    94.             {
    95.                 air_pressure_at_sea_level = "",
    96.                 air_temperature = "",
    97.                 cloud_area_fraction = "",
    98.                 precipitation_amount = "",
    99.                 relative_humidity = "",
    100.                 wind_from_direction = "",
    101.                 wind_speed = "",
    102.             },
    103.         };
    104.  
    105.         Next12Hours next12hours = new Next12Hours()
    106.         {
    107.             summary = new Summary()
    108.             {
    109.                 symbol_code = "",
    110.             },
    111.         };
    112.  
    113.         Next1Hours next1hours = new Next1Hours()
    114.         {
    115.             summary = new Summary()
    116.             {
    117.                 symbol_code = "",
    118.             },
    119.             details = new Details()
    120.             {
    121.                 air_pressure_at_sea_level = 1,
    122.                 air_temperature = 1,
    123.                 cloud_area_fraction = 1,
    124.                 relative_humidity = 1,
    125.                 wind_from_direction = 1,
    126.                 wind_speed = 1,
    127.                 precipitation_amount = 1,
    128.             },
    129.         };
    130.  
    131.         Next6Hours next6hours = new Next6Hours()
    132.         {
    133.             summary = new Summary()
    134.             {
    135.                 symbol_code = "",
    136.             },
    137.             details = new Details()
    138.             {
    139.                 air_pressure_at_sea_level = 1,
    140.                 air_temperature = 1,
    141.                 cloud_area_fraction = 1,
    142.                 relative_humidity = 1,
    143.                 wind_from_direction = 1,
    144.                 wind_speed = 1,
    145.                 precipitation_amount = 1,
    146.             },
    147.         };
    148.  
    149.         Properties properties = new Properties()
    150.         {
    151.             meta = new Meta()
    152.             {
    153.                 updated_at = DateTime.Now,
    154.                 units = new Units()
    155.                 {
    156.                     air_pressure_at_sea_level = "",
    157.                     air_temperature = "",
    158.                     cloud_area_fraction = "",
    159.                     precipitation_amount = "",
    160.                     relative_humidity = "",
    161.                     wind_from_direction = "",
    162.                     wind_speed = "",
    163.                 },
    164.             },
    165.             timeseries = new List<Timeseries>(),
    166.         };
    167.  
    168.         Root root = new Root()
    169.         {
    170.             type = "",
    171.             geometry = new Geometry()
    172.             {
    173.                 type = "",
    174.                 coordinates = new List<double>(),
    175.             },
    176.             properties = new Properties()
    177.             {
    178.                 meta = new Meta()
    179.                 {
    180.                     updated_at = DateTime.Now,
    181.                     units = new Units()
    182.                     {
    183.                         air_pressure_at_sea_level = "",
    184.                         air_temperature = "",
    185.                         cloud_area_fraction = "",
    186.                         precipitation_amount = "",
    187.                         relative_humidity = "",
    188.                         wind_from_direction = "",
    189.                         wind_speed = "",
    190.                     },
    191.                 },
    192.                 timeseries = new List<Timeseries>(),
    193.             },
    194.         };
    195.  
    196.         Summary summary = new Summary()
    197.         {
    198.             symbol_code = "",
    199.         };
    200.  
    201.         Timeseries timeseries = new Timeseries()
    202.         {
    203.             time = DateTime.Now,
    204.             data = new Data()
    205.             {
    206.                 instant = new Instant()
    207.                 {
    208.                     details = new Details()
    209.                     {
    210.                         air_pressure_at_sea_level = 1,
    211.                         air_temperature = 1,
    212.                         cloud_area_fraction = 1,
    213.                         relative_humidity = 1,
    214.                         wind_from_direction = 1,
    215.                         wind_speed = 1,
    216.                         precipitation_amount = 1,
    217.                     },
    218.                 },
    219.                 next_12_hours = new Next12Hours()
    220.                 {
    221.                     summary = new Summary()
    222.                     {
    223.                         symbol_code = "",
    224.                     },
    225.                 },
    226.                 next_1_hours = new Next1Hours()
    227.                 {
    228.                     summary = new Summary()
    229.                     {
    230.                         symbol_code = "",
    231.                     },
    232.                     details = new Details()
    233.                     {
    234.                         air_pressure_at_sea_level = 1,
    235.                         air_temperature = 1,
    236.                         cloud_area_fraction = 1,
    237.                         relative_humidity = 1,
    238.                         wind_from_direction = 1,
    239.                         wind_speed = 1,
    240.                         precipitation_amount = 1,
    241.                     },
    242.                 },
    243.                 next_6_hours = new Next6Hours()
    244.                 {
    245.                     summary = new Summary()
    246.                     {
    247.                         symbol_code = "",
    248.                     },
    249.                     details = new Details()
    250.                     {
    251.                         air_pressure_at_sea_level = 1,
    252.                         air_temperature = 1,
    253.                         cloud_area_fraction = 1,
    254.                         relative_humidity = 1,
    255.                         wind_from_direction = 1,
    256.                         wind_speed = 1,
    257.                         precipitation_amount = 1,
    258.                     },
    259.                 },
    260.             },
    261.         };
    262.  
    263.         Units units = new Units()
    264.         {
    265.             air_pressure_at_sea_level = "",
    266.             air_temperature = "",
    267.             cloud_area_fraction = "",
    268.             precipitation_amount = "",
    269.             relative_humidity = "",
    270.             wind_from_direction = "",
    271.             wind_speed = "",
    272.         };
    273.     }
    However I do not understand how I should use this structure. Have tested with jsonUtility, the only one I used in the past.

    Can someone nice recommend me how to create code to use this structure?

    Here is an example of the json input data: https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=60.10&lon=9.58
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Json.net is my recommended json library.

    Code (CSharp):
    1. Data data =JsonConvert.DeserializeObject<Data>(yourJsonDataString);
    Will create an instance of your Data class and populate it with data defined in your string.
     
    pKallv likes this.
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    Thanks, that worked, now I have another problem. I will submit a new post about that.
     
    Kurt-Dekker likes this.