0

I want to use mapbox matching in ASP.NET Core. This link you can get response https://api.mapbox.com/matching/v5/mapbox/driving/..

I want to convert this response to dynamic json in Asp.net core, I use this line

var jsonResponse = JsonConvert.DeserializeObject(mapResponse);

but I get empty values. Any help?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mu nt
  • 93
  • 1
  • 1
  • 9

1 Answers1

1

Firstly, The API you have shared I got follwing response using postman:

enter image description here

If its same what you are getting then I follow below steps to retrive the value from the API response in C# asp.net core controller

Model You should have :

public class Admin
        {
            public string iso_3166_1_alpha3 { get; set; }
            public string iso_3166_1 { get; set; }
        }

        public class Leg
        {
            public List<object> via_waypoints { get; set; }
            public List<Admin> admins { get; set; }
            public double weight { get; set; }
            public double duration { get; set; }
            public List<object> steps { get; set; }
            public double distance { get; set; }
            public string summary { get; set; }
        }

        public class Matching
        {
            public double confidence { get; set; }
            public string weight_name { get; set; }
            public double weight { get; set; }
            public double duration { get; set; }
            public double distance { get; set; }
            public List<Leg> legs { get; set; }
            
            public string geometry { get; set; }
        }

        public class Tracepoint
        {
            public int matchings_index { get; set; }
            public int waypoint_index { get; set; }
            public int alternatives_count { get; set; }
            public double distance { get; set; }
            public string name { get; set; }
            public List<double> location { get; set; }
        }

        public class MapResponseClass
        {
            public List<Matching> matchings { get; set; }
            public List<Tracepoint> tracepoints { get; set; }
            public string code { get; set; }
            public string uuid { get; set; }
        }

Asp.net core Controller:

public async Task<IActionResult> CallMapAPI()
        {

           try
            {
                HttpClient client = new HttpClient();
                HttpResponseMessage response = await client.GetAsync("https://api.mapbox.com/matching/v5/mapbox/driving/-117.17282,32.71204;-117.17288,32.71225;-117.17293,32.71244;-117.17292,32.71256;-117.17298,32.712603;-117.17314,32.71259;-117.17334,32.71254?access_token=pk.eyJ1Ijoibm92ZXJzbWFwIiwiYSI6ImNreTdwc3ppNTE3dzkyb3B2MnVzNXpueTUifQ.csYTL2GKkl99Yqk_TQjr5w");
                response.EnsureSuccessStatusCode();
                string mapAPIjson = await response.Content.ReadAsStringAsync();
             
                var data = JsonConvert.DeserializeObject<MapResponseClass>(mapAPIjson);
            }
            catch (Exception ex)
            {

                throw;
            }

            

            return data;
        }}

Output:

enter image description here

Note:

You should bound your class as per your API response. What I am assuming of your empty values is you haven't converted the relevant class accordingly. I hope above steps guided you accordingly.

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
  • Why we have to specify type in JsonConvert.DeserializeObject<.Net type> ? – Mu nt Feb 16 '22 at 16:17
  • Yes if you want to get the value as I have shown above but you needn't do anything if you want to get the data as `json string` as like this line ` string mapAPIjson = await response.Content.ReadAsStringAsync();` If you are okay with `mapAPIjson` as output then no need to explicitly class conversion instead yes you have to map with the class. Hope it explans clearly now. – Md Farid Uddin Kiron Feb 17 '22 at 01:20