I have very little experience of json and never before used Newtonsoft json.net.
I have installed json.net and got it to work somewhat following advise from @Brathnann in this post: How to access this json class
This is a pretty large json and I am trying to load it into a List but get the following error:
JsonReaderException: Error parsing comment. Expected: *, got U. Path '', line 1, position 1.
I have the following class structure called YrModel generated via https://json2csharp.com/
[Serializable]
public class YrModel
{
public class Root
{
public string type { get; set; }
public Geometry geometry { get; set; }
public Properties properties { get; set; }
}
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; }
}
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; }
}
public class Geometry
{
public string type { get; set; }
public List<double> coordinates { get; set; }
}
public class Instant
{
public Details details { get; set; }
}
public class Meta
{
public DateTime updated_at { get; set; }
public Units units { get; set; }
}
public class Next12Hours
{
public Summary summary { get; set; }
}
public class Next1Hours
{
public Summary summary { get; set; }
public Details details { get; set; }
}
public class Next6Hours
{
public Summary summary { get; set; }
public Details details { get; set; }
}
public class Properties
{
public Meta meta { get; set; }
public List<Timeseries> timeseries { get; set; }
}
public class Summary
{
public string symbol_code { get; set; }
}
public class Timeseries
{
public DateTime time { get; set; }
public Data data { get; set; }
}
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; }
}
}
Given results from googling I have tried a more slimmer model with only the properties I need but get the same result.
In my last tests I used the following code to try to read the json into a List:
void Test2()
{
path = JSON_FileHandler.GetPath("myjsonBack.json");
if (File.Exists(path))
{
var _my_List = JsonConvert.DeserializeObject<YrModel>(path);
}
}
This is an extract of the json file:
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
9.58,
60.1,
496.0
]
},
"properties": {
"meta": {
"updated_at": "2022-09-28T07:36:26Z",
"units": {
"air_pressure_at_sea_level": "hPa",
"air_temperature": "celsius",
"cloud_area_fraction": "%",
"precipitation_amount": "mm",
"relative_humidity": "%",
"wind_from_direction": "degrees",
"wind_speed": "m/s"
}
},
"timeseries": [
{
"time": "2022-09-28T08:00:00Z",
"data": {
"instant": {
"details": {
"air_pressure_at_sea_level": 1004.4,
"air_temperature": 8.0,
"cloud_area_fraction": 100.0,
"relative_humidity": 96.2,
"wind_from_direction": 60.5,
"wind_speed": 3.8,
"precipitation_amount": 0.0
}
},
"next_12_hours": {
"summary": {
"symbol_code": "lightrain"
}
},
"next_1_hours": {
"summary": {
"symbol_code": "lightrain"
},
"details": {
"air_pressure_at_sea_level": 0.0,
"air_temperature": 0.0,
"cloud_area_fraction": 0.0,
"relative_humidity": 0.0,
"wind_from_direction": 0.0,
"wind_speed": 0.0,
"precipitation_amount": 0.3
}
},
"next_6_hours": {
"summary": {
"symbol_code": "rain"
},
"details": {
"air_pressure_at_sea_level": 0.0,
"air_temperature": 0.0,
"cloud_area_fraction": 0.0,
"relative_humidity": 0.0,
"wind_from_direction": 0.0,
"wind_speed": 0.0,
"precipitation_amount": 1.7
}
}
}
}
]
}
}
I have validated the array @ https://jsonlint.com/