How to read AJAX Feed API results from C # code?

I want to use the Goolge AJAX Feed API in my C # console application to save return channels as C # collections, so I can use this .net collcionion in my .net application.

I noticed that google gives snippets of Java code, but I don't know how to encode it in C #. I know that there is a very good open-source.net Json.NET library that we can use to read and write JSON formatted data.

Can someone give me exmpale how to use C # and Json.NET to play with Google's AJAX Feed API?

Final decision:

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);
+3
1

, .

, :

// 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
}

, , , "" , 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)

...

, !

+4

Source: https://habr.com/ru/post/1716951/


All Articles