第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Web api 響應(yīng)中的額外花括號(hào)使響應(yīng)無法轉(zhuǎn)換為模型

Web api 響應(yīng)中的額外花括號(hào)使響應(yīng)無法轉(zhuǎn)換為模型

C#
桃花長(zhǎng)相依 2022-10-15 14:13:35
我正在嘗試閱讀 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;        }    }我從這個(gè) api 得到的響應(yīng)嵌入在花括號(hào)中,因此它不允許我將響應(yīng)轉(zhuǎn)換為 Yelp 模型。響應(yīng)的開頭和結(jié)尾有一對(duì)額外的花括號(hào)。如何以正確的 Json 格式獲取響應(yīng)。
查看完整描述

3 回答

?
慕的地8271018

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

的呼喚


all.ReadAsAsync<Object>().Result;

JObject返回一個(gè)不能通過簡(jiǎn)單轉(zhuǎn)換轉(zhuǎn)換為的實(shí)例Yelp。而是ReadAsAsync像這樣調(diào)用


var business = await all.ReadAsAsync<Common.Models.Yelp.Yelp>();

return business;

如果你仍然想調(diào)用它,object你可以這樣做


var business = await all.ReadAsAsync<object>();

return ((JObject)business).ToObject<Yelp>();

筆記


響應(yīng) json 不包含額外的花括號(hào)。它只是JObject將它們添加到調(diào)試視圖中。通過檢查將響應(yīng)讀取為字符串的結(jié)果很容易檢查這一點(diǎn)all.ReadAsStringAsync().Result。


查看完整回答
反對(duì) 回復(fù) 2022-10-15
?
嗶嗶one

TA貢獻(xiàn)1854條經(jīng)驗(yàn) 獲得超8個(gè)贊

使用 JSON.NET 的Deserialize 方法使用泛型類型重載將字符串反序列化為所需的 POCO。

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;

    }

}


public async Task<Common.Models.Yelp.Yelp> GetAllBusiness(decimal latitude, decimal longitude)

{

    HttpContent 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;

    }



    string responseBody = await all.ReadAsStringAsync();


    // Deserialize from serialized string into your POCO

    var business = JsonConvert.DeserializeObject<Common.Models.Yelp.Yelp>(responseBody);

    return business;

}




查看完整回答
反對(duì) 回復(fù) 2022-10-15
?
幕布斯7119047

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊

var business = all.Result;  

 var resultString = business.ReadAsStringAsync();  

 return JsonConvert.DeserializeObject<Common.Models.Yelp.Yelp>(resultString);


查看完整回答
反對(duì) 回復(fù) 2022-10-15
  • 3 回答
  • 0 關(guān)注
  • 149 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)