2

Ok, so I am using MVC 3 and it is great at de-serializing a JSON data set into a strongly typed object that is passed to my controller action. Unfortunately I have not found a solution to a more dynamic case.

Does the built in Json de-serialisation and classes have support for an "undefined" property set? For example lets say I have some fixed data like name and age, but I also want to pass down a dynamically created rating list where the user could enter (or select) a movie and set a rating value in a table.

The model data structure could be something like this:

public class UserRatings
{
  public string Name { get; set; }
  public int Age { get; set; }
  public Dictionary<string,int> Ratings { get; set; }
}

But assuming that my Json dataset looks like this from javascript:

var data = { Name: name, Age: age, Ratings: rating };

Where the rating variable is a dynamically constructed object that consist of the name (or id) of of the movie as key and rating as number. Naturally de-serialization of this in the controller action will not be successful as it does not understand the mapping of Ratings to the rather complex dictionary object. But is there a generic Json collection that I could use instead for Ratings as an intermediate format?

I have tried making the Ratings object a Json string in javascript and just sending a string down, but I am unable to find a "factory" or something that can make a Json structure in C# that I can iterate over to get the data out. The classes Json and JsonResult does not help me in this regard it seems. Basically how can I use the built in Json support in MVC to do my own de-serialisation into some generic Json collection object?

2
  • Just wanted to comment that my solution so far is to make Ratings a string type, and do a JSON.stringify of the ratings object on the client and sending that as one string attribute. They are then all mapped directly into the POCO. I then strip off all the escape characters of the rating string which leaves me with a json string that the JavaScriptSerialiser is able to turn into a Dictionary<string,object> where I can iterate over them and update the database. Its a bit messy, but it works. Any suggestions is greatly appreciated. Commented Mar 30, 2011 at 12:31
  • Related/dupe of: stackoverflow.com/questions/5022958/… stackoverflow.com/questions/10787679/… Commented Jan 7, 2013 at 10:14

3 Answers 3

4

You Could Use JavaScriptSerializer or DataContractSerializer with Some ActionFilters. They're very flexible

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. JavaScriptSerializer has a method called DeserializeObject which in my case actually creates a Dictionary<string,object> of the Json data which simplifies the whole process greatly! I guess I'd wish this to be part of the built in de-serializer as creating a same typed dictionary will not work for the complex property in question.
1

See my answer Passing dynamic json object to C# MVC controller basically using a dynamic type and a ValueProviderFactory is the cleanest way to deserialize Json to something more dynamic.

Comments

0

There's another option which i prefer using for being neater...(we eliminate the step of getting data from the request stream)

Here's a code sample

Cat catObj = new Cat();

if (TryUpdateModel<Cat>(catObj))
{
//do stuff
}
else
{
//invalid input
}

The TryUpdateModel resides in the controller namespace and hence no need to add any additional reference.

If you just need the Json sent in as part of the request you could obtain it using the following block of code(you could also obtain it from Request.Form)

using (StreamReader reader = new StreamReader(Request.InputStream))
{
    var inputJson = reader.ReadToEnd();
}

3 Comments

Ok, so this would be to avoid having a simple string attribute to contain all the dynamic json data in my case? But I still need some way to get hold of that jason block that contains my dynamic list of things, any idea how to do this in the action?
But as you might have seen from my example I am sending one json object that contains some attributes that should map directly into the properties of a well defined object, and one attribute that contains a generic name-value map. Is there a datatype I could use on that property that would be populated with the raw json data from this attribute? Atm I need to send a json encoded string as one datatype and unescape this string before using the JavaScriptSerializer to get a dictionary out of this. It would be nice to avoid this stringified-json-hack-in-json if possible.
@Johncl - you could use a Dictionay<string,Dynamic> to make it look neater...but remember it uses reflection and reflection code can be slower...

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.