Freigeben über


Convert Rss to JSON - Serialize to JSON by using XmlSerialization and JavaScriptSerializer

JSON (Javascript Object Notation) – is a light weight data interchange format. It essentially is composed of key-value pairs. For Example –

 XML

<rss version="2.0">

  <channel>

    <item>

      <link>https://test.com</link>

      <title>Page Title</title>

    </item>

  </channel>

</rss>

JSON -

{
"Version":"2.0",
"Channel": {
"Items": [{
"Link":"https://test.com",
"Title":"Page Title"
}]
}
}

The beauty of JSON is it integrates easily with Javascript. Just use eval() function of JavaScript to parse. So to initialize the above JSON this should do the trick –

var rss = eval(‘({ "Version":"2.0", "Channel": { "Items": [{Link":"https://test.com",Title":"Page Title" }]}}’);

Javascript does provide some level of Object orientation. So you could access the properties from the above object like –

var itemtitle = rss.Channel.Items[0].Title;
var rssVersion = rss.Version;

RSS (Really Simple Syndication) – is used to syndicate your content. Unless you were hibernating for the past 5 years, I’m sure you know what Rss is. :)


So let’s get to the meat of this article. How to Convert an Rss Feed to JSON?

First I will use XmlSerialization to Serialize/Deserialize Rss. The System.Xml.Serialization namespace is used for this purpose. You will need to decorate all your properties with XmlSerialization attributes. E.g. –

[XmlElement("description")]

public string Description

{

get

      {

      return _description;

      }

      set

      {

      _description = value;

      }

}

In this example (code provided below) I have created classes closely matching Rss specification (https://blogs.law.harvard.edu/tech/rss)

To Serialize you can use the Serialize() method of XmlSerializer class

private string Serialize()

{

string xml = string.Empty;

      using (StringWriter output = new StringWriter(new StringBuilder(), System.Globalization.CultureInfo.InvariantCulture))

      {

      XmlSerializer xmlSerializer = new XmlSerializer(typeof(RssDocument));

            xmlSerializer.Serialize(output, this);

            xml = output.ToString();

      }

      return xml;

}

To Deserialize you can use the Deserialize ) method of XmlSerializer class

private static T DeserializeFromXmlUsingStringReader<T>(string xml)

{

if (string.IsNullOrEmpty(xml))

      {

      throw new ArgumentException("xml");

      }

      XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

      using (StringReader strngReader = new StringReader(xml))

      {

      return (T)xmlSerializer.Deserialize(strngReader);

      }

}

This should create a Strongly typed representation of Rss.

To convert this Strongly typed Rss classes to Json format is actually very easy. ASP.Net AJAX provides JavaScriptSerializer class in System.Web.Script.Serialization.JavascriptSerializer.

Of particular importance to this example in this table is Serialize(object) method.

public string ToJson()

{

System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();

      return js.Serialize(this); //this is an instance of the strongly typed Rss Class

}


What’s cool is this technique can be used on any objects which uses XmlSerialization.

I have code example in the attachment below with example on how to Convert Rss to Json by pointing to a Url or loading from Xml string.

Note : To run the example you need to have ASP.Net 2.0 and Ajax Extensions. You can download it from here

 

RssToJson.zip

Comments