Web api 响应中的额外花括号使响应无法转换为模型

我正在尝试阅读 Yelp API。下面是我的代码。


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;

        }

    }


我从这个 api 得到的响应嵌入在花括号中,因此它不允许我将响应转换为 Yelp 模型。

响应的开头和结尾有一对额外的花括号。如何以正确的 Json 格式获取响应。


桃花长相依
浏览 101回答 3
3回答

慕的地8271018

的呼唤all.ReadAsAsync<Object>().Result;JObject返回一个不能通过简单转换转换为的实例Yelp。而是ReadAsAsync像这样调用var business = await all.ReadAsAsync<Common.Models.Yelp.Yelp>();return business;如果你仍然想调用它,object你可以这样做var business = await all.ReadAsAsync<object>();return ((JObject)business).ToObject<Yelp>();笔记响应 json 不包含额外的花括号。它只是JObject将它们添加到调试视图中。通过检查将响应读取为字符串的结果很容易检查这一点all.ReadAsStringAsync().Result。

哔哔one

使用 JSON.NET 的Deserialize 方法使用泛型类型重载将字符串反序列化为所需的 POCO。public async Task<HttpContent> InvokeApi(string path, HttpAction action, HttpContent content = null, TimeSpan? overrideTimeout = null, string externalServer = null){&nbsp; &nbsp; var sUrl = externalServer == null ? ServerUrl : externalServer;&nbsp; &nbsp; using (var client = new HttpClient())&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; client.BaseAddress = new Uri(sUrl);&nbsp; &nbsp; &nbsp; &nbsp; if (overrideTimeout.HasValue)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; client.Timeout = overrideTimeout.Value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //this.Log("Connecting to {0} Api at {1}".Fmt(WebPortalServer, ServerUrl));&nbsp; &nbsp; &nbsp; &nbsp; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", AccessToken);&nbsp; &nbsp; &nbsp; &nbsp; client.DefaultRequestHeaders.Accept.Clear();&nbsp; &nbsp; &nbsp; &nbsp; client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));&nbsp; &nbsp; &nbsp; &nbsp; HttpResponseMessage response;&nbsp; &nbsp; &nbsp; &nbsp; switch (action)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case HttpAction.Get:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = await client.GetAsync(path);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case HttpAction.Post:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = await client.PostAsync(path, content);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case HttpAction.Put:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = await client.PutAsync(path, content);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case HttpAction.Delete:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = await client.DeleteAsync(path);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new ArgumentOutOfRangeException("action", action, null);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return response.IsSuccessStatusCode ? response.Content : null;&nbsp; &nbsp; }}public async Task<Common.Models.Yelp.Yelp> GetAllBusiness(decimal latitude, decimal longitude){&nbsp; &nbsp; HttpContent all = await _webPortalApiClient.InvokeApi($"businesses/search?limit=10&latitude={latitude}&longitude={longitude}", HttpAction.Get, null, null, "https://api.yelp.com/v3/");&nbsp; &nbsp; if (all == null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp; string responseBody = await all.ReadAsStringAsync();&nbsp; &nbsp; // Deserialize from serialized string into your POCO&nbsp; &nbsp; var business = JsonConvert.DeserializeObject<Common.Models.Yelp.Yelp>(responseBody);&nbsp; &nbsp; return business;}

幕布斯7119047

var business = all.Result;&nbsp;&nbsp;&nbsp;var resultString = business.ReadAsStringAsync();&nbsp;&nbsp;&nbsp;return JsonConvert.DeserializeObject<Common.Models.Yelp.Yelp>(resultString);
打开App,查看更多内容
随时随地看视频慕课网APP