I try to parse a response by the OSRM map matching service using the following code (here a .NET Fiddle too):
namespace OsrmMapMatch
{
public class OsrmResponse
{
public string code;
public OsrmLeg[] legs;
}
public class OsrmLeg
{
public OsrmAnnotation annotation;
}
public class OsrmAnnotation
{
public uint[] nodes;
}
internal class Program
{
static async Task Main(string[] args)
{
const string HttpClientMapMatch = "HttpClientMapMatch";
const string OsrmUri = "10.757938,52.437444;10.764379,52.437314;10.770562,52.439067;10.773268,52.436633?overview=simplified&radiuses=50;50;50;50&generate_hints=false&skip_waypoints=true&gaps=ignore&annotations=nodes&geometries=geojson";
ServiceProvider serviceProvider = new ServiceCollection()
.AddHttpClient(HttpClientMapMatch, httpClient =>
{
httpClient.BaseAddress = new Uri("https://router.project-osrm.org/match/v1/driving/");
}).Services.BuildServiceProvider();
IHttpClientFactory httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
HttpClient httpClient = httpClientFactory.CreateClient(HttpClientMapMatch);
HttpResponseMessage response = await httpClient.GetAsync(OsrmUri);
if (response.IsSuccessStatusCode)
{
string stringData = await response.Content.ReadAsStringAsync();
OsrmResponse osrmResponse = JsonSerializer.Deserialize<OsrmResponse>(stringData);
Console.WriteLine($"OsrmResponse: {JsonSerializer.Serialize(osrmResponse)}");
}
}
}
}
Unfortunately, the returned object is empty, its code
and legs
properties are null:
I have tried the following change with no success:
JsonSerializerOptions options = new()
{
PropertyNameCaseInsensitive = true
};
OsrmResponse osrmResponse = JsonSerializer.Deserialize<OsrmResponse>(stringData, options);
What could be the reason for the failing JSON parsing?
How do you debug such problems in general?
I would like to use Microsoft’s System.Text.Json and do not want to use Newtonsoft.JSON.
I only need the OSM nodes
array at the end, that is why I have omitted other members in the response.
2
Answers
Based on the response, your model should look like below. The simplest way to generate the model is using
Visual Studio->Edit->Paste Special->Paste Json as Classes
or any other Online tools like json2csharp. Once you update the model your code should work.Your problem is that the Json you want to serialize is different from how you want to serialize you. You have to fix the class you want to use to serial your response Json.