我想在我的 C# 控制台应用程序中使用Goolge AJAX Feed API将返回的提要保存为 C# 集合,这样我就可以在我的 .net 应用程序中使用这个 .net collcetion。
我注意到谷歌提供了一个 Java 访问代码片段,但我不知道如何用 C# 对其进行编码。我知道有一个非常好的 .net 开源库Json.NET,我们可以使用它来读取和写入 JSON 格式的数据。
有人可以给我一个例子,如何使用 C# 和Json.NET与 Google AJAX Feed API 一起玩吗?
最终解决方案:
public class FeedApiResult
{
public ResponseData responseData { get; set; }
public string responseDetails { get; set; }
public string responseStatus { get; set; }
}
public class ResponseData
{
public Feed feed { get; set; }
}
public class Feed
{
public string title { get; set; }
public string link { get; set; }
public string author { get; set; }
public string description { get; set; }
public string type { get; set; }
public List<Entry> entries { get; set; }
}
public class Entry
{
public string title { get; set; }
public string link { get; set; }
public string author { get; set; }
public string publishedDate { get; set; }
public string contentSnippet { get; set; }
public string content { get; set; }
}
var url = "http://ajax.googleapis.com/ajax/services/feed/load?q=http%3A%2F%2Fwww.digg.com%2Frss%2Findex.xml&v=1.0";
var wc = new WebClient();
var rawFeedData = wc.DownloadString(url);
//You can use System.Web.Script.Serialization if you don't want to use Json.NET
JavaScriptSerializer ser = new JavaScriptSerializer();
FeedApiResult foo = ser.Deserialize<FeedApiResult>(rawFeedData);
//Json.NET also return you the same strong typed object
var apiResult = JsonConvert.DeserializeObject<FeedApiResult>(rawFeedData);