JSON: desereialization

So as I said in my two previous posts JSON: the first wave and JSON introduction, I wanted a solution to avoid creating a wrapper for a model that most of the time was useless code stuffing my project.

So I saw this:

_responseObj=JsonConvert.DeserializeObject<Dictionary<string, object>>(_jsonSample);

This allowed me to break the first object in to its pieces, then I could access it by its key and deseralize it as a useful model

List<ResultEntity> result = new List<ResultEntity>();
string resultsJson= _responseObj["responseData"].ToString();
var res = JsonConvert.DeserializeObject<ResultsEntity>(resultsJson);

If you want to see it in action you can get the code at my GitHub

Happy coding and remember…there is life beyond coding 🙂

JSON introduction

I used to work a lot with desktop applications, so I was used to work with some xml configuration files and serialization, but since recently everything has changed for me I decided I’d give a try to the web stack.

So I’ll start from my very own experience and admit that I had no idea how JSON worked and what to do with it.

At my new job one of the requirement was to build a basic web api that consume data(JSON formatted of course) from another web service, cook it and serve it to the consumers of my web service. I was confused at my first approach.

Lets see a typical JSON response:

{
  "responseData": {
    "results": [
      {
        "GsearchResultClass": "GwebSearch",
        "unescapedUrl": "http: //en.wikipedia.org/wiki/Paris_Hilton",
        "url": "http: //en.wikipedia.org/wiki/Paris_Hilton",
        "visibleUrl": "en.wikipedia.org",
        "cacheUrl": "http: //www.google.com/search?q=cache:TwrPfhd22hYJ:en.wikipedia.org",
        "title": "Paris Hilton - Wikipedia, the free encyclopedia",
        "titleNoFormatting": "Paris Hilton - Wikipedia, the free encyclopedia",
        "content": "[1] In 2006, she released her debut album..."
      },
      {
        "GsearchResultClass": "GwebSearch",
        "unescapedUrl": "http: //www.imdb.com/name/nm0385296/",
        "url": "http: //www.imdb.com/name/nm0385296/",
        "visibleUrl": "www.imdb.com",
        "cacheUrl": "http: //www.google.com/search?q=cache:1i34KkqnsooJ:www.imdb.com",
        "title": "Paris Hilton",
        "titleNoFormatting": "Paris Hilton",
        "content": "Self: Zoolander. Socialite Paris Hilton..."
      }
    ],
    "cursor": {
      "pages": [
        {
          "start": "0",
          "label": 1
        },
        {
          "start": "4",
          "label": 2
        },
        {
          "start": "8",
          "label": 3
        },
        {
          "start": "12",
          "label": 4
        }
      ],
      "estimatedResultCount": "59600000",
      "currentPageIndex": 0,
      "moreResultsUrl":
      "http: //www.google.com/search?oe=utf8&ie=utf8..."
    }
  },
  "responseDetails": null,
  "responseStatus": 200
}

Wow! that’s quite a lot to take in for a novice like me.

It has a responseData node that has a results node that contains a list. Quiet a tree structure and believe me I could be much worse.

So at the following posts we will see step by step how I worked this out