Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't receive data from API using c# httpClient

I want to receive the data from this link: https://rapidapi.com/DIlyanBarbov/api/crypto-news-live/

API

This is my objects:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

This is my async Task to get them:

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

When I stop debugger at this line:

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

He stuck and does not move forward.

The body looks like:

[{"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"}]

I don’t know if my objects are as they should be.

Also, when deserializing, I’m not sure if that should be the case.

Can I get a working example ?

>Solution :

With a model such as

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

You would expect the JSON output to be:

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

But the JSON you’ve posted in your question doesn’t have a parent object called CryptoNews, and is simply the List<NewsBody>.

Deserialize the JSON as such instead:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading