I have following JSON file:
[
{"sport": "CYCLING_SPORT"},
{"source": "TRACK_MOBILE"},
{"created_date": "2016-03-28 12:00:00.0"},
{"start_time": "2016-03-28 12:00:00.0"},
{"end_time": "2016-03-28 13:00:00.0"},
{"duration_s": 3600},
{"distance_km": 10.123412312412},
{"calories_kcal": 1000.12},
{"altitude_min_m": 100.5},
{"altitude_max_m": 200.5},
{"speed_avg_kmh": 30.21314151234123},
{"speed_max_kmh": 50.1234},
{"hydration_l": 1.142124},
{"ascend_m": 200.9},
{"descend_m": 200.9},
{"points": [
[
{"location": [[
{"latitude": 40.1234},
{"longitude": 40.1234}
]]},
{"distance_km": 0},
{"timestamp": "Mon Mar 28 12:00:00 UTC 2016"}
],
[
{"location": [[
{"latitude": 50.1234},
{"longitude": 50.1234}
]]},
{"distance_km": 10.1234},
{"timestamp": "Mon Mar 28 13:00:00 UTC 2016"}
]
]}
]
I generated classes to hold data:
public class Workout
{
public string sport { get; set; }
public string source { get; set; }
public string created_date { get; set; }
public string start_time { get; set; }
public string end_time { get; set; }
public int duration_s { get; set; }
public double? distance_km { get; set; }
public double? calories_kcal { get; set; }
public double? altitude_min_m { get; set; }
public double? altitude_max_m { get; set; }
public double? speed_avg_kmh { get; set; }
public double? speed_max_kmh { get; set; }
public double? hydration_l { get; set; }
public double? ascend_m { get; set; }
public double? descend_m { get; set; }
public Point[][] points { get; set; }
}
public class Point
{
public Location[][] location { get; set; }
public double? distance_km { get; set; }
public string? timestamp { get; set; }
public double? altitude { get; set; }
public double? speed_kmh { get; set; }
}
public class Location
{
public double latitude { get; set; }
public double longitude { get; set; }
}
I'm trying to parse JSON using following code:
using System;
using System.Text.Json;
namespace Reader
{
class Program
{
static void Main(string[] args)
{
var json = System.IO.File.ReadAllText(@"<path_to_file>");
var workout = JsonSerializer.Deserialize<Workout[]>(json)[0];
System.Console.Write(workout.sport);
System.Console.Write(workout.points.Length);
}
}
}
Unfortunately, this is what I get while I'm debugging (and of course null reference exception):
One value looks good, but everything else is empty. Can someone give me a hint what I am doing wrong?
EDIT:
I can't edit JSON directly, but I can modify it before deserializing. So I did following:
var builder = new StringBuilder(System.IO.File.ReadAllText(@"<path_to_file>"));
builder.Replace("{", "");
builder.Replace("}", "");
builder[0] = '{';
builder[builder.Length - 1] = '}';
var json = builder.ToString();
System.Console.Write(json);
var workout = JsonSerializer.Deserialize<Workout>(json);
System.Console.Write(workout.sport);
System.Console.Write(workout.points.Length);
And now I get this:
{
"sport": "CYCLING_SPORT",
"source": "TRACK_MOBILE",
"created_date": "2016-03-28 12:00:00.0",
"start_time": "2016-03-28 12:00:00.0",
"end_time": "2016-03-28 13:00:00.0",
"duration_s": 3600,
"distance_km": 10.123412312412,
"calories_kcal": 1000.12,
"altitude_min_m": 100.5,
"altitude_max_m": 200.5,
"speed_avg_kmh": 30.21314151234123,
"speed_max_kmh": 50.1234,
"hydration_l": 1.142124,
"ascend_m": 200.9,
"descend_m": 200.9,
"points": [
[
"location": [[
"latitude": 40.1234,
"longitude": 40.1234
]],
"distance_km": 0,
"timestamp": "Mon Mar 28 12:00:00 UTC 2016"
],
[
"location": [[
"latitude": 50.1234,
"longitude": 50.1234
]],
"distance_km": 10.1234,
"timestamp": "Mon Mar 28 13:00:00 UTC 2016"
]
]
}
But deserializer is still not happy:
System.Text.Json.JsonException: 'The JSON value could not be converted to Point[]. Path: $.points[0][0] | LineNumber: 18 | BytePositionInLine: 22.'