5

let's say I have the following JSON payload;

  {
   "pagemap": {
    "metatags": [
     {
      "msapplication-task": "name\u003dAbout Tugberk Ugurlu;action-uri\u003d/about;icon-uri\u003d/content/App_Icons/icos/about.ico",
      "msapplication-task": "name\u003dContact;action-uri\u003d/contact;icon-uri\u003d/content/App_Icons/icos/contact.ico",
      "msapplication-task": "name\u003dBlog RSS Feed;action-uri\u003dhttp://feeds.feedburner.com/TugberkUgurlu;icon-uri\u003d/content/App_Icons/icos/rss.ico",
      "msapplication-task": "name\u003dTugberk on Twitter;action-uri\u003dhttp://twitter.com/tourismgeek;icon-uri\u003d/content/App_Icons/icos/twitter.ico",
      "msapplication-starturl": "./",
      "application-name": "Tugberk's Blog",
      "msapplication-tooltip": "bla bla bla..."
     }
    ]
   }
  }

The property names under mettags are dynamic. I mean one of them is msapplication-starturl for this request but it might be msapplication-foo for another.

So what would be the best c# classes for this kind of JSON payload?

EDIT

this is the part of JSON format which google search API gives. Also I am using Json.NET. is There any other way than dynamic?

7
  • The only somewhat somewhat-typed way that can be statically represented represented is IDictionary<JSONKey,JSONValueSuperType> (as a sequence element of metatags) or similar -- of course, if this dynamic nature only applies to "msapplication-" ... but why make it even more confusing? :) I'm honestly not really sure what is being asked otherwise :-/ Commented Jun 1, 2011 at 20:59
  • @pst this is the part of JSON format which google search API gives. Also I am using Json.NET. There isn't any other way than dynamic, is it? Commented Jun 1, 2011 at 21:05
  • @pst I think the question is pretty clear. Also it's very easy to understand that the metatags contains the page metetags. so it will be highly possible that they will be different. Commented Jun 1, 2011 at 21:10
  • @tugberk Then see my comment. Alternatively, consider a JSON mechanism that allows/uses dynamic. Commented Jun 1, 2011 at 21:44
  • 2
    Doesn't json require the keys in a dictionary to be unique? Is that even valid json? Commented Jun 1, 2011 at 21:49

2 Answers 2

2

I'd probably just want the MetaTags array to just get pushed into a Dictionary<string,string> or even just List<string> and then write a helper class that parses msapplication-task values into something you want.

Edit: I believe the OP is looking for some help in how his model class would actually be

public class PageMap
{
    public Dictionary<string,string> MetaTags {get;set; }
}

From looking at that json object, it appears that RestSharp should be able to deserialize it into this class.

Calling code would be similar to

var client = new RestClient("somegoogle.com");
var request = new RestRequest("Some/Foo/Bar", Method.GET)
                                 { RequestFormat = DataFormat.Json }; 
request.AddParameter("p1", "quigybo");
client.Execute<PageMan>(request)
Sign up to request clarification or add additional context in comments.

4 Comments

@tugberk - Dictionary<TKey, TValue> is a class.
@Greg so it would be this : public Dictionary<string, string> Metatags { get; set; } ?
IMO a public setter for a property that has a mutable type(like Dictionary) is rarely a good idea. I'd make the the setter private.
@CodeInChaos the OP would need to verify whether that is a viable option for JSON.NET serialization (that RestSharp uses)
2

You should look at JSON.NET and JObject for building dynamic loosely typed objects. If you decide to use it, you should NuGet to download it.

Example:

var client = new WebClient();
client.Headers.Add("User-Agent", "your user agent here");
var response = client.DownloadString(new Uri("http://www.domain.com/source-page.html"));
JObject jo = JObject.Parse(response);

2 Comments

+1 for mentioning direct JObects, RestSharp is biult on top of JSON.NET so the OP has that implicitly.
I had never heard of JObject. Cheers mate, very useful response :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.