如何从 C# 代码读取 Google AJAX Feed API 结果?
我想在我的 C# 中使用 Goolge AJAX Feed API控制台应用程序将返回源保存为 C# 集合,以便我可以在我的 .net 应用程序中使用此 .net 集合。
我注意到 google 提供了 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);
I want to use Goolge AJAX Feed API in my C# console application to save return feeds as C# collection so i can use this .net collcetion in my .net application.
I noticed google give a Java Access Code Snippets but I have no idea how to coding it in C#.
I know there is a very good .net opensource library Json.NET we can use to read and write JSON formatted data.
Can someone give me an exmpale how to use C# and Json.NET play with Google AJAX Feed API?
Final Solution:
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚看了这些例子,这就是我的做法。
例如,一个未经测试的快速 hack:
但是,如果您想要强类型类,则必须首先创建一个“看起来像”从 feed api 首先返回的数据的类,即
然后在步骤#3中,您可以使用这样的反序列化方法:
...
希望这有帮助!
I've just looked at the examples, and here is how I'd go about it.
For example, a quick untested hack:
If however, you want strongly-typed class, you must first make a class that "looks like" the data that is returned from the feed api first, i.e.
Then in step #3, you can use the deserializing method like this:
...
Hope this helps!