I have used simple json structures with jsonutility but now I have, IMHO, a complex json structure:
[Serializable]
public class Root
{
public string type { get; set; }
public Geometry geometry { get; set; }
public Properties properties { get; set; }
}
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
[Serializable]
public class Data
{
public Instant instant { get; set; }
public Next12Hours next_12_hours { get; set; }
public Next1Hours next_1_hours { get; set; }
public Next6Hours next_6_hours { get; set; }
}
[Serializable]
public class Details
{
public double air_pressure_at_sea_level { get; set; }
public double air_temperature { get; set; }
public double cloud_area_fraction { get; set; }
public double relative_humidity { get; set; }
public double wind_from_direction { get; set; }
public double wind_speed { get; set; }
public double precipitation_amount { get; set; }
}
[Serializable]
public class Geometry
{
public string type { get; set; }
public List<double> coordinates { get; set; }
}
[Serializable]
public class Instant
{
public Details details { get; set; }
}
[Serializable]
public class Meta
{
public DateTime updated_at { get; set; }
public Units units { get; set; }
}
[Serializable]
public class Next12Hours
{
public Summary summary { get; set; }
}
[Serializable]
public class Next1Hours
{
public Summary summary { get; set; }
public Details details { get; set; }
}
[Serializable]
public class Next6Hours
{
public Summary summary { get; set; }
public Details details { get; set; }
}
[Serializable]
public class Properties
{
public Meta meta { get; set; }
public List<Timeseries> timeseries { get; set; }
}
[Serializable]
public class Summary
{
public string symbol_code { get; set; }
}
[Serializable]
public class Timeseries
{
public DateTime time { get; set; }
public Data data { get; set; }
}
[Serializable]
public class Units
{
public string air_pressure_at_sea_level { get; set; }
public string air_temperature { get; set; }
public string cloud_area_fraction { get; set; }
public string precipitation_amount { get; set; }
public string relative_humidity { get; set; }
public string wind_from_direction { get; set; }
public string wind_speed { get; set; }
}
I have added the “[Serializable]”.
I have used the json2csharp.com website to generate this.
I used the same site to generate “example”:
Data data = new Data()
{
instant = new Instant()
{
details = new Details()
{
air_pressure_at_sea_level = 1,
air_temperature = 1,
cloud_area_fraction = 1,
relative_humidity = 1,
wind_from_direction = 1,
wind_speed = 1,
precipitation_amount = 1,
},
},
next_12_hours = new Next12Hours()
{
summary = new Summary()
{
symbol_code = "",
},
},
next_1_hours = new Next1Hours()
{
summary = new Summary()
{
symbol_code = "",
},
details = new Details()
{
air_pressure_at_sea_level = 1,
air_temperature = 1,
cloud_area_fraction = 1,
relative_humidity = 1,
wind_from_direction = 1,
wind_speed = 1,
precipitation_amount = 1,
},
},
next_6_hours = new Next6Hours()
{
summary = new Summary()
{
symbol_code = "",
},
details = new Details()
{
air_pressure_at_sea_level = 1,
air_temperature = 1,
cloud_area_fraction = 1,
relative_humidity = 1,
wind_from_direction = 1,
wind_speed = 1,
precipitation_amount = 1,
},
},
};
Details details = new Details()
{
air_pressure_at_sea_level = 1,
air_temperature = 1,
cloud_area_fraction = 1,
relative_humidity = 1,
wind_from_direction = 1,
wind_speed = 1,
precipitation_amount = 1,
};
Geometry geometry = new Geometry()
{
type = "",
coordinates = new List<double>(),
};
Instant instant = new Instant()
{
details = new Details()
{
air_pressure_at_sea_level = 1,
air_temperature = 1,
cloud_area_fraction = 1,
relative_humidity = 1,
wind_from_direction = 1,
wind_speed = 1,
precipitation_amount = 1,
},
};
Meta meta = new Meta()
{
updated_at = DateTime.Now,
units = new Units()
{
air_pressure_at_sea_level = "",
air_temperature = "",
cloud_area_fraction = "",
precipitation_amount = "",
relative_humidity = "",
wind_from_direction = "",
wind_speed = "",
},
};
Next12Hours next12hours = new Next12Hours()
{
summary = new Summary()
{
symbol_code = "",
},
};
Next1Hours next1hours = new Next1Hours()
{
summary = new Summary()
{
symbol_code = "",
},
details = new Details()
{
air_pressure_at_sea_level = 1,
air_temperature = 1,
cloud_area_fraction = 1,
relative_humidity = 1,
wind_from_direction = 1,
wind_speed = 1,
precipitation_amount = 1,
},
};
Next6Hours next6hours = new Next6Hours()
{
summary = new Summary()
{
symbol_code = "",
},
details = new Details()
{
air_pressure_at_sea_level = 1,
air_temperature = 1,
cloud_area_fraction = 1,
relative_humidity = 1,
wind_from_direction = 1,
wind_speed = 1,
precipitation_amount = 1,
},
};
Properties properties = new Properties()
{
meta = new Meta()
{
updated_at = DateTime.Now,
units = new Units()
{
air_pressure_at_sea_level = "",
air_temperature = "",
cloud_area_fraction = "",
precipitation_amount = "",
relative_humidity = "",
wind_from_direction = "",
wind_speed = "",
},
},
timeseries = new List<Timeseries>(),
};
Root root = new Root()
{
type = "",
geometry = new Geometry()
{
type = "",
coordinates = new List<double>(),
},
properties = new Properties()
{
meta = new Meta()
{
updated_at = DateTime.Now,
units = new Units()
{
air_pressure_at_sea_level = "",
air_temperature = "",
cloud_area_fraction = "",
precipitation_amount = "",
relative_humidity = "",
wind_from_direction = "",
wind_speed = "",
},
},
timeseries = new List<Timeseries>(),
},
};
Summary summary = new Summary()
{
symbol_code = "",
};
Timeseries timeseries = new Timeseries()
{
time = DateTime.Now,
data = new Data()
{
instant = new Instant()
{
details = new Details()
{
air_pressure_at_sea_level = 1,
air_temperature = 1,
cloud_area_fraction = 1,
relative_humidity = 1,
wind_from_direction = 1,
wind_speed = 1,
precipitation_amount = 1,
},
},
next_12_hours = new Next12Hours()
{
summary = new Summary()
{
symbol_code = "",
},
},
next_1_hours = new Next1Hours()
{
summary = new Summary()
{
symbol_code = "",
},
details = new Details()
{
air_pressure_at_sea_level = 1,
air_temperature = 1,
cloud_area_fraction = 1,
relative_humidity = 1,
wind_from_direction = 1,
wind_speed = 1,
precipitation_amount = 1,
},
},
next_6_hours = new Next6Hours()
{
summary = new Summary()
{
symbol_code = "",
},
details = new Details()
{
air_pressure_at_sea_level = 1,
air_temperature = 1,
cloud_area_fraction = 1,
relative_humidity = 1,
wind_from_direction = 1,
wind_speed = 1,
precipitation_amount = 1,
},
},
},
};
Units units = new Units()
{
air_pressure_at_sea_level = "",
air_temperature = "",
cloud_area_fraction = "",
precipitation_amount = "",
relative_humidity = "",
wind_from_direction = "",
wind_speed = "",
};
}
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