0

I am fetching an api which successfully returns a json string, but when I try to parse it into objects, it doesnt work.

I am using webRequest to pull the api string. I think the problem might be with how the json string is formatted [see last code snippet]. then this is the main method [this is where I need help on]. the program always writes "its null".

 using (var webClient = new WebClient())
        {
        String rawJSON = webClient.DownloadString("https://newsapi.org/v2/everything?q=apple&from=2019-09-14&sortBy=publishedAt&apiKey=fc771cbf668b44a4a20437df65fc89d2");
        NewsCollection newsAnchor = JsonConvert.DeserializeObject<NewsCollection>(rawJSON);
        System.Diagnostics.Debug.WriteLine("starting test");
        if (newsAnchor.all_news == null)
            System.Diagnostics.Debug.WriteLine("its null");
        else
            System.Diagnostics.Debug.WriteLine(newsAnchor.all_news.Count);
        System.Diagnostics.Debug.WriteLine("ending test");
        }

this is my NewsCollection Class:

public class NewsCollection
{
    public List<NewsClass> all_news;

    public List<NewsClass> All_news { get => all_news; set => all_news = value; }

    public int return_size()
    {
        return all_news.Count;
    }
}

NewsClass:

public class NewsClass
{
    string source;
    string author;
    string title;
    string description;
    string url;
    string urlToImage;
    string published;
    string content;

    public string Source { get => source; set => source = value; }
    public string Author { get => author; set => author = value; }
    public string Title { get => title; set => title = value; }
    public string Description { get => description; set => description = value; }
    public string Url { get => url; set => url = value; }
    public string UrlToImage { get => urlToImage; set => urlToImage = value; }
    public string Published { get => published; set => published = value; }
    public string Content { get => content; set => content = value; }

}

and most importantly. this is part of the json. this is the way the json file comes in. Maybe it has something to do with how its formatted.

{"status":"ok","totalResults":5826,"articles":[{"source": 
{"id":null,"name":"Newsbtc.com"},"author":"Nick Chong","title":"Crypto 
Tidbits: Bitcoin ETF Denied, Libra Loses Visa & eBay, SEC Crackdown on 
Telegram’s Blockchain","description":"Another week, another of Crypto 
Tidbits. This week, Bitcoin traded within a relatively wide range — $8,200 
to $8,900 — but is closing at a very similar level to last week. Analysts 
are currently divided over what this consolidation means for the 
cryptocurrenc…","url":"https://www.newsbtc.com/2019/10/12/crypto-tidbits- 
bitcoin-etf-libra-visa-ebay-sec-telegram- 
blockchain/","urlToImage":"https://www.newsbtc.com/wp- 
content/uploads/2019/09/shutterstock_371007224- 
1200x780.jpg","publishedAt":"2019-10-12T20:30:45Z","content":"Another 
week, another of Crypto Tidbits. This week, Bitcoin traded within a 
relatively wide range $8,200 to $8,900 but is closing at a very similar 
level to last week.  Analysts are currently divided over what this 
consolidation means for the cryptocurrency m… [+5147 chars]"}]}
GioPoe
  • 109
  • 3
  • 12

0 Answers0