簡體   English   中英

無法使用 c# httpClient 從 API 接收數據

[英]Can't receive data from API using c# httpClient

我想從這個鏈接接收數據: https://rapidapi.com/DIlyanBarbov/api/crypto-news-live/

API

這是我的對象:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace SmartCryptoWorld.Models
{
    public class News
    {
        public List<NewsBody> CryptoNews { get; set; }
    }

    public class NewsBody
    {
        [JsonProperty("title")]
        public string TitleNews { get; set; }

        [JsonProperty("url")]
        public string UrlNews { get; set; }
    }
}

這是我獲取它們的異步任務:

private async Task GetAPI()
    {
        var client = new HttpClient();
        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Get,
            RequestUri = new Uri("https://crypto-news-live.p.rapidapi.com/news/coindesk"),
            Headers =
        {
            { "x-rapidapi-host", "crypto-news-live.p.rapidapi.com" },
            { "x-rapidapi-key", "51569aba99mshf9e839fcfce791bp16c0dbjsn9ced6dba7472" },
        },
        };
        using (var response = await client.SendAsync(request))
        {
            News news = new News();
            response.EnsureSuccessStatusCode();
            var body = await response.Content.ReadAsStringAsync();
            news = JsonConvert.DeserializeObject<News>(body);
            var test = news;
        }
    }

當我在這一行停止調試器時:

news = JsonConvert.DeserializeObject<News>(body);

他卡住了,沒有前進。

body看起來像:

[{"title":"Crypto Explainer+","url":"https://coindesk.com/learn/"},{"title":"Crypto Explainer+","url":"https://coindesk.com/learn/"},{"title":"Crypto Prices ","url":"https://coindesk.com/data/"},{"title":"First Mover Asia: Crypto Rally Fizzles Amid Widespread Inflationary Concerns","url":"https://coindesk.com/markets/2021/12/16/first-mover-asia-crypto-rally-fizzles-amid-widespread-inflationary-concerns/"},{"title":"Japan’s Crypto Exchanges Grapple With Travel Rule as Deadline Looms","url":"https://coindesk.com/policy/2021/12/17/japans-crypto-exchanges-grapple-with-travel-rule-as-deadline-looms/"},{"title":"First Mover Asia: Crypto Rally Fizzles Amid Widespread Inflationary Concerns","url":"https://coindesk.com/markets/2021/12/16/first-mover-asia-crypto-rally-fizzles-amid-widespread-inflationary-concerns/"},{"title":"Should Crypto and Porn Get Intimate?","url":"https://coindesk.com/layer2/2021/12/16/should-crypto-and-porn-get-intimate/"},{"title":"Federal Regulator Says Credit Unions Can Partner With Crypto Providers","url":"https://coindesk.com/policy/2021/12/16/federal-regulator-says-credit-unions-can-partner-with-crypto-providers/"},{"title":"Robinhood Adds Crypto Gifting Feature","url":"https://coindesk.com/business/2021/12/16/robinhood-to-offer-users-ability-to-gift-crypto/"},{"title":"Community Crypto","url":"https://coindesk.com/tv/community-crypto"},{"title":"Japan’s Crypto Exchanges Grapple With Travel Rule as Deadline Looms","url":"https://coindesk.com/policy/2021/12/17/japans-crypto-exchanges-grapple-with-travel-rule-as-deadline-looms/"},{"title":"Why the Marijuana Industry Needs Crypto","url":"https://coindesk.com/layer2/2021/12/15/why-the-marijuana-industry-needs-crypto/"},{"title":"Should Crypto and Porn Get Intimate?","url":"https://coindesk.com/layer2/2021/12/16/should-crypto-and-porn-get-intimate/"},{"title":"What the Fed’s ‘Hawkish’ Turn Means for Bitcoin","url":"https://coindesk.com/podcasts/the-breakdown-with-nlw/what-the-feds-hawkish-turn-means-for-bitcoin"},{"title":"Crypto 2022: Culture and Entertainment Week","url":"https://coindesk.com/layer2/culture-week/"},{"title":"Behind the Scenes of El Salvador’s Bitcoin Bond With the Man Who Designed It","url":"https://coindesk.com/layer2/2021/12/16/behind-the-scenes-of-el-salvadors-bitcoin-bond-with-the-man-who-designed-it/"},{"title":"Crypto 2022: Culture and Entertainment Week","url":"https://coindesk.com/layer2/culture-week/"},{"title":"Mobile Bitcoin Gaming Is Powering Up on Lightning","url":"https://coindesk.com/layer2/culture-week/2021/12/15/mobile-bitcoin-gaming-is-powering-up-on-lightning/"},{"title":"Markets Daily Crypto Roundup","url":"https://coindesk.com/podcasts/markets-daily/"},{"title":"Crypto News Roundup for Dec. 16, 2021","url":"https://coindesk.com/podcasts/markets-daily/crypto-news-roundup-for-dec-16-2021"},{"title":"First Mover Asia: Crypto Rally Fizzles Amid Widespread Inflationary Concerns","url":"https://coindesk.com/markets/2021/12/16/first-mover-asia-crypto-rally-fizzles-amid-widespread-inflationary-concerns/"},{"title":"What the Fed’s ‘Hawkish’ Turn Means for Bitcoin","url":"https://coindesk.com/podcasts/the-breakdown-with-nlw/what-the-feds-hawkish-turn-means-for-bitcoin"},{"title":"Behind the Scenes of El Salvador’s Bitcoin Bond With the Man Who Designed It","url":"https://coindesk.com/layer2/2021/12/16/behind-the-scenes-of-el-salvadors-bitcoin-bond-with-the-man-who-designed-it/"},{"title":"Bitcoin Oversold Near $46K Support; Resistance at $55K","url":"https://coindesk.com/markets/2021/12/16/bitcoin-oversold-near-46k-support-resistance-at-55k/"},{"title":"What Is Bitcoin?","url":"https://coindesk.com/learn/what-is-bitcoin/"},{"title":"First Mover Asia: Crypto Rally Fizzles Amid Widespread Inflationary Concerns","url":"https://coindesk.com/markets/2021/12/16/first-mover-asia-crypto-rally-fizzles-amid-widespread-inflationary-concerns/"},{"title":"ConsenSys Collaborates With Mastercard on New Ethereum Scaling System","url":"https://coindesk.com/business/2021/12/16/consensys-collaborates-with-mastercard-on-new-ethereum-scaling-solution/"},{"title":"Ethereum Privacy Startup Aztec Raises $17M in Paradigm-Led Series A","url":"https://coindesk.com/business/2021/12/16/ethereum-privacy-startup-aztec-raises-17m-in-paradigm-led-series-a/"},{"title":"Solana Leads Gains as Cryptos Recover on Fed Decision","url":"https://coindesk.com/markets/2021/12/16/solana-leads-gains-as-cryptos-recover-on-fed-decision/"},{"title":"First Mover Asia: Fed Decision on Stimulus Money Buoys Crypto Markets","url":"https://coindesk.com/markets/2021/12/15/first-mover-asia-fed-decision-on-stimulus-money-buoys-crypto-markets/"},{"title":"Fed Speeds Up Stimulus Withdrawal, and Bitcoin Jumps","url":"https://coindesk.com/markets/2021/12/15/fed-speeds-up-stimulus-withdrawal-an-extra-headwind-for-bitcoin/"},{"title":"Just How Bearish Are the Bitcoin and Crypto Communities?","url":"https://coindesk.com/podcasts/the-breakdown-with-nlw/just-how-bearish-are-the-bitcoin-and-crypto-communities"},{"title":"Tezos ‘Exchange-Traded Cryptocurrency’ Launches on German Exchange","url":"https://coindesk.com/markets/2021/12/13/tezos-exchange-traded-cryptocurrency-launches-on-german-exchange/"},{"title":"The Three Types of Crypto Investors","url":"https://coindesk.com/business/2021/12/09/the-three-types-of-crypto-investors/"},{"title":"Should Crypto and Porn Get Intimate?","url":"https://coindesk.com/layer2/2021/12/16/should-crypto-and-porn-get-intimate/"},{"title":"Robinhood Adds Crypto Gifting Feature","url":"https://coindesk.com/business/2021/12/16/robinhood-to-offer-users-ability-to-gift-crypto/"},{"title":"Bitcoin Investor Peter McCormack Buys UK Soccer Club, Has Big Plans to Improve It","url":"https://coindesk.com/business/2021/12/16/bitcoin-investor-peter-mccormack-buys-uk-soccer-club-with-incredibly-ambitious-plans-to-improve/"},{"title":"Fed Speeds Up Stimulus Withdrawal, and Bitcoin Jumps","url":"https://coindesk.com/markets/2021/12/15/fed-speeds-up-stimulus-withdrawal-an-extra-headwind-for-bitcoin/"},{"title":"Robinhood Working on New Crypto Gifting Feature: Report","url":"https://coindesk.com/business/2021/12/13/robinhood-working-on-new-crypto-gifting-feature-report/"},{"title":"Tezos ‘Exchange-Traded Cryptocurrency’ Launches on German Exchange","url":"https://coindesk.com/markets/2021/12/13/tezos-exchange-traded-cryptocurrency-launches-on-german-exchange/"},{"title":"US Inflation Jumps to 39-Year High of 6.8% in November; Bitcoin Falls","url":"https://coindesk.com/markets/2021/12/10/us-inflation-rate-surges-to-68-in-november-a-4-decade-high-bitcoin-jumps/"},{"title":"First Mover Asia: Crypto Rally Fizzles Amid Widespread Inflationary Concerns","url":"https://coindesk.com/markets/2021/12/16/first-mover-asia-crypto-rally-fizzles-amid-widespread-inflationary-concerns/"},{"title":"Bitcoin Oversold Near $46K Support; Resistance at $55K","url":"https://coindesk.com/markets/2021/12/16/bitcoin-oversold-near-46k-support-resistance-at-55k/"},{"title":"Should Crypto and Porn Get Intimate?","url":"https://coindesk.com/layer2/2021/12/16/should-crypto-and-porn-get-intimate/"},{"title":"Robinhood Adds Crypto Gifting Feature","url":"https://coindesk.com/business/2021/12/16/robinhood-to-offer-users-ability-to-gift-crypto/"},{"title":"Japan’s Crypto Exchanges Grapple With Travel Rule as Deadline Looms","url":"https://coindesk.com/policy/2021/12/17/japans-crypto-exchanges-grapple-with-travel-rule-as-deadline-looms/"},{"title":"Federal Regulator Says Credit Unions Can Partner With Crypto Providers","url":"https://coindesk.com/policy/2021/12/16/federal-regulator-says-credit-unions-can-partner-with-crypto-providers/"},{"title":"US Claims Bitcoin Mining Crown Following China Crackdown","url":"https://coindesk.com/business/2021/10/13/us-claims-bitcoin-mining-crown-following-china-crackdown/"},{"title":"The Investor’s Perspective on the Bitcoin Taproot Upgrade","url":"https://coindesk.comhttps://downloads.coindesk.com/research/Investor-Perspective-on-Bitcoin-Taproot-Upgrade.pdf"}]

我不知道我的對象是否應有的樣子。

此外,在反序列化時,我不確定是否應該如此。

我可以得到一個工作示例嗎?

用 model 如

namespace SmartCryptoWorld.Models
{
    public class News
    {
        public List<NewsBody> CryptoNews { get; set; }
    }

    public class NewsBody
    {
        [JsonProperty("title")]
        public string TitleNews { get; set; }

        [JsonProperty("url")]
        public string UrlNews { get; set; }
   }
}

您會期望 JSON output 為:

{
    cryptoNews: [
    {
        "TitleNews": "...",
        "UrlNews": "..."
    },
    ...
    ]
}

但是您在問題中發布的 JSON 沒有名為CryptoNewsparent object ,而只是List<NewsBody>

像這樣反序列化JSON

var news = new News();
var newsBody = JsonConvert.DeserializeObject<List<NewsBody>>(body);
news.CryptoNews = newsBody;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM