Reputation: 7854
I have the following JSON that was obtained after calling JSON.stringify from my javascript
{"parameters":"[{\"ParamName\":\"@s\",\"ParamValue\":\"12\"},{\"ParamName\":\"@t\",\"ParamValue\":\"21\"}]"}
How do i map this to the following model in my ASP.Net MVC2 controller
public class SCVM
{
public string content { get; set; }
public string type { get; set; }
public List<Parameters> parameters { get; set; }
public SCVM()
{
parameters = new List<Parameters>();
}
}
public class Parameters
{
public string ParamName { get; set; }
public string ParamValue { get; set; }
}
I am trying to get this in either a dictionary format or a list objects, but finding difficult to work it out the right way.
Upvotes: 0
Views: 186
Reputation: 7817
If your json would look like this: it probably should. (see my comment on your question)
{
"parameters":[
{
"ParamName":"@s",
"ParamValue":"12"
},
{
"ParamName":"@t",
"ParamValue":"21"
}
]
}
with this piece of javascript you'll create the correct json:
var parametersCollection = {
parameters: []
};
function QueryParameters(paramName, paramValue) { this.ParamName = paramName; this.ParamValue = paramValue; }
parametersCollection.parameters.push(new QueryParameters("@s", "12"));
parametersCollection.parameters.push(new QueryParameters("@t", "21"));
var json = JSON.stringify(parametersCollection);
you could deserialize is with Json.NET see here like so:
SCVM scvm = JsonConvert.DeserializeObject<SCVM>(json);
where json is a string with the json formatted like I showed
Here is an excellent tutorial on how to post json data to an MVC2 site using jquery. You wouldn't even have to use the Json.NET lib to deserialize your json.
Upvotes: 1