I am trying to read Yelp API. Below is my code.
public async Task<HttpContent> InvokeApi(string path, HttpAction action, HttpContent content = null, TimeSpan? overrideTimeout = null, string externalServer = null)
{
var sUrl = externalServer == null ? ServerUrl : externalServer;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(sUrl);
if (overrideTimeout.HasValue)
{
client.Timeout = overrideTimeout.Value;
}
//this.Log("Connecting to {0} Api at {1}".Fmt(WebPortalServer, ServerUrl));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", AccessToken);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response;
switch (action)
{
case HttpAction.Get:
response = await client.GetAsync(path);
break;
case HttpAction.Post:
response = await client.PostAsync(path, content);
break;
case HttpAction.Put:
response = await client.PutAsync(path, content);
break;
case HttpAction.Delete:
response = await client.DeleteAsync(path);
break;
default:
throw new ArgumentOutOfRangeException("action", action, null);
}
return response.IsSuccessStatusCode ? response.Content : null;
}
}
I am calling the above Function as
public async Task<Common.Models.Yelp.Yelp> GetAllBusiness(decimal latitude, decimal longitude)
{
var all = await _webPortalApiClient.InvokeApi($"businesses/search?limit=10&latitude={latitude}&longitude={longitude}", HttpAction.Get, null, null, "https://api.yelp.com/v3/");
if (all == null)
{
return null;
}
//var business = await all.ReadAsAsync<Common.Models.Yelp.Yelp>();
var business = all.ReadAsAsync<Object>().Result;
var result = (Common.Models.Yelp.Yelp)(business);
return result;
}
The response I am getting from this api is embedded in curly braces, because of this it is not allowing me to convert response to Yelp Model.
Her is the response I get.
{{ "businesses": [ { "id": "Xg-FyjVKAN70LO4u4Z1ozg", "alias": "hog-island-oyster-co-san-francisco", "name": "Hog Island Oyster Co", "image_url": "", "is_closed": false, "url": "", "review_count": 5550, "categories": [ { "alias": "seafood", "title": "Seafood" }, { "alias": "seafoodmarkets", "title": "Seafood Markets" }, { "alias": "raw_food", "title": "Live/Raw Food" } ], "rating": 4.5, "coordinates": { "latitude": 37.795831, "longitude": -122.393303 }, "transactions": [], "price": "$$", "location": { "address1": "1 Ferry Bldg", "address2": "", "address3": "Shop 11", "city": "San Francisco", "zip_code": "94111", "country": "US", "state": "CA", "display_address": [ "1 Ferry Bldg", "Shop 11", "San Francisco, CA 94111" ] }, "phone": "+14153917117", "display_phone": "(415) 391-7117", "distance": 1154.8167382059307 }, { "id": "PsY5DMHxa5iNX_nX0T-qPA", "alias": "kokkari-estiatorio-san-francisco", "name": "Kokkari Estiatorio", "image_url": "", "is_closed": false, "url": "", "review_count": 4300, "categories": [ { "alias": "greek", "title": "Greek" }, { "alias": "mediterranean", "title": "Mediterranean" } ], "rating": 4.5, "coordinates": { "latitude": 37.796996, "longitude": -122.399661 }, "transactions": [ "pickup" ], "price": "$$$", "location": { "address1": "200 Jackson St", "address2": "", "address3": "", "city": "San Francisco", "zip_code": "94111", "country": "US", "state": "CA", "display_address": [ "200 Jackson St", "San Francisco, CA 94111" ] }, "phone": "+14159810983", "display_phone": "(415) 981-0983", "distance": 1124.9562174585888 }, { "id": "ZoZjbOYR-apY8XvommlNUA", "alias": "the-house-san-francisco", "name": "The House", "image_url": "": false, "url": "", "review_count": 4521, "categories": []}}
There is a pair of extra curly braces in the start and the end of the response. How can I fetch response in proper Json format.