如何从 C# 代码读取 Google AJAX Feed API 结果?

发布于 2024-08-03 11:14:49 字数 1893 浏览 14 评论 0原文

我想在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星光不落少年眉 2024-08-10 11:14:49

我刚刚看了这些例子,这就是我的做法。

  1. 构造提要 Url(阅读文档)
  2. 使用 WebClient 到 以字符串形式下载 URL
  3. 使用Json.NET读取字符串。
  4. 使用 for 循环读取每个条目

例如,一个未经测试的快速 hack:

// 1.
var url = "'http://ajax.googleapis.com/ajax/services/feed/load?q=http%3A%2F%2Fwww.digg.com%2Frss%2Findex.xml&v=1.0";

// 2.
var wc = new WebClient();
var rawFeedData = wc.DownloadString(url);

// 3.
var feedContent = JObject.Parse(rawFeedData);

// ...
var entries = feedContent["entries"];

for (int i = 0; i < entries.Length; i++) {
    var entry = entries[i];

    // insert entry into your desired collection
}

但是,如果您想要强类型类,则必须首先创建一个“看起来像”从 feed api 首先返回的数据的类,即

public class FeedApiResult {
    public FeedApiFeedObj responseData { get; set; }
    // snip ...
}

public class FeedApiFeedObj {
    public string title { get; set; }
    public string link { get; set; }
    // snip ...
}

然后在步骤#3中,您可以使用这样的反序列化方法:

var apiResult = JsonConvert.DeserializeObject<FeedApiResult>(feedContent)

...

希望这有帮助!

I've just looked at the examples, and here is how I'd go about it.

  1. Construct the feed Url (read the documentation)
  2. Use the WebClient to Download the URL as a String.
  3. Use Json.NET to reads the string.
  4. Use a for-loop to read each entries

For example, a quick untested hack:

// 1.
var url = "'http://ajax.googleapis.com/ajax/services/feed/load?q=http%3A%2F%2Fwww.digg.com%2Frss%2Findex.xml&v=1.0";

// 2.
var wc = new WebClient();
var rawFeedData = wc.DownloadString(url);

// 3.
var feedContent = JObject.Parse(rawFeedData);

// ...
var entries = feedContent["entries"];

for (int i = 0; i < entries.Length; i++) {
    var entry = entries[i];

    // insert entry into your desired collection
}

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.

public class FeedApiResult {
    public FeedApiFeedObj responseData { get; set; }
    // snip ...
}

public class FeedApiFeedObj {
    public string title { get; set; }
    public string link { get; set; }
    // snip ...
}

Then in step #3, you can use the deserializing method like this:

var apiResult = JsonConvert.DeserializeObject<FeedApiResult>(feedContent)

...

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文