我正在获取一个成功返回json字符串的api,但是当我尝试将其解析为对象时,它不起作用。
我正在使用webRequest提取api字符串。我认为问题可能出在json字符串的格式如何[请参阅最后的代码片段]。然后这是主要方法[这是我需要帮助的地方]。该程序始终写入“其空值”。
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");
}
这是我的NewsCollection类:
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; }
}
,最重要的是。这是json的一部分。这是json文件进入的方式。也许与它的格式有关。
{"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]"}]}