Posts tagged ‘Jayrock’

Json in C#

I did some Json work in C#, I hadn’t done any Json with C# before.  Naturally I go googling without much luck at first.  When you look for Json libraries and code snippets they are plentiful, but most of them java script browser stuff.  Then a friend pointed out Jayrock which was exactly what I needed.  It can be used in C# code to address Json feed similar to Xml feeds.

As mentioned before json is extremely java script friendly.  Let’s take a look at one of the Google examples.

<h3>Upcoming Google Developer Events</h3>
<div id=“agenda”></div>
<script>
  function listEvents(root) {
    var feed = root.feed;
    var entries = feed.entry || [];
    var html = [‘<ul>’];

    for (var i = 0; i < entries.length; ++i) {
      var entry = entries[i];
      var title = entry.title.$t;
      var start = (entry[‘gd$when’]) ? entry[‘gd$when’][0].startTime : “”;     
      html.push(‘<li>’, start, ‘ – ‘, title, ‘</li>’);
    }
    html.push(‘</ul>’);
    document.getElementById(“agenda”).innerHTML = html.join(“”);
  }
</script>

<script src=“http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json-in-script&callback=listEvents”>
</script>

Run the sample.

And then in C# using Jayrock

using Jayrock.Json;

WebClient client = new WebClient();
Uri url = new Uri(“http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json”);
string gevents= client.DownloadString(url);

using (JsonTextReader reader = new JsonTextReader(new StringReader(gevents)))
{

    while (reader.Read())
    {
        if (reader.Depth == 4 &&
            reader.TokenClass == JsonTokenClass.Member &&
            reader.Text != null )
        {
            if (reader.Text.Equals(“gd$when”))
            {
                // navigate to the right node
                for( int i = 0; i < 6; i++)
                    reader.Read();
               
                // startTime
                Console.WriteLine(reader.ReadString());
            }
            else if (reader.Text.Equals(“title”))
            {
                // navigate to the right node
                for (int i = 0; i < 5; i++)
                    reader.Read();

                // $t
                Console.WriteLine(reader.ReadString());
            }
        }
    }
}

Those for loops are a bit ugly to get down the tree to the correct node. There has to be a better way of doing that. With simple data pairs it’s no problem but with heavier feeds like the one I took in the sample it’s not as obvious. If you have a simpler or more elegant way of doing it in C# drop a comment.

This is what the Json feed looks like.

Here are some tools, that I came up on JsonViewer which is hosted on CodePlex. The viewer also comes with a plugin for Fiddler, which is really handy and also has one for VS. Fiddler is a http debugging proxy. CodePlex is Microsoft’s answer to sourceforge.

You can find extensive list of json libraries here, for bunch of different languages.