我一直在尝试这样做一段时间,但没有很好的结果。我的 obj.title 调用似乎返回一个空字符串。
到目前为止,我有:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Test2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString("https://newsapi.org/v1/articles?source=business-insider&sortBy=top&apiKey=f47672429b7044a29e7a4671f9f41c28");
var obj = JsonConvert.DeserializeObject<MyClass>(json);
label.Content = obj.title;
}
}
}
public class MyClass
{
public string title { get; set; }
}
}
我正在使用的网址是 https://newsapi.org/v1/articles?source=business-insider&sortBy=top&apiKey=f47672429b7044a29e7a4671f9f41c28
因此,您的 JSON 源返回一个如下所示的对象:
{ "状态": "确定", "来源": "商业内幕", "排序依据": "顶部", "文章":[{ "作者": "艾伦·史密斯", "标题":"道德专家说,特朗普与他的商业帝国保持距离的最新举动是不够的:"没有任何重大改变", "描述":"特朗普说他将'不做新交易',一旦他上任,将把他的商业帝国的控制权交给他的两个成年儿子和高管。 "网址": "http://www.businessinsider.com/trump-conflicts-of-interest-latest-move-2016-12", "urlToImage": "http://static2.businessinsider.com/image/584f0536a1a45e1a008b53ba-1190-625/ethics-experts-say-trumps-latest-move-to-distance-himself-from-his-business-empire-is-not-enough-nothing-of-consequence-has-changed.jpg", "发布于":"2016-12-13T22:55:20Z" }, { "作者": "帕梅拉·恩格尔", "标题":"'任何人想要的只是不比动物更好的待遇':随着阿勒颇的沦陷,叙利亚人失去了希望", "描述":"叙利亚城市阿勒颇大部分落入政权军手中,让许多反对巴沙尔·阿萨德总统残暴统治的人感到绝望。 "网址": "http://www.businessinsider.com/aleppo-siege-assad-syria-2016-12", "urlToImage": "http://static2.businessinsider.com/image/58508de0ca7f0cc2178b4e05-1190-625/all-anyone-ever-wanted-was-to-be-treated-no-better-than-animals-syrians-lose-hope-as-aleppo-falls.jpg", "发布于":"2016-12-13T23:34:31Z" }]//由于重复性而修剪 }
JSON 对象没有属性title
。它有一个数组articles
每个数组都有一个title
.
您需要如下所示的类:
public class SomeArticles
{
public List<Article> Articles{get;set;}
}
和
public class Article
{
public string Title{get;set;}
}
那么你会
var someArticles = JsonConvert.DeserializeObject<SomeArticles>(json);
然后
var firstArticleTitle = someArticles.Articles.First().Title;
你可以做这样的事情:
void Main()
{
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString("https://newsapi.org/v1/articles?source=business-insider&sortBy=top&apiKey=f47672429b7044a29e7a4671f9f41c28");
var obj = JsonConvert.DeserializeObject<RootObject>(json);
Console.WriteLine(obj.articles[0].title);
}
}
public class Article
{
public string author { get; set; }
public string title { get; set; }
public string description { get; set; }
public string url { get; set; }
public string urlToImage { get; set; }
public string publishedAt { get; set; }
}
public class RootObject
{
public string status { get; set; }
public string source { get; set; }
public string sortBy { get; set; }
public List<Article> articles { get; set; }
}
问候
反序列化的类应该是
public class MyClass
{
public Article[] articles { get; set; }
}
public class Article
{
public string title { get; set; }
}
从 JSON 结构来看,根对象似乎没有标题变量。 有一系列文章,每篇文章都有自己的标题。