0

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.

2
  • there is something wrong with your json. your parameters aren't stored as seperate objects but as a string. paste your javascript code aswell. Commented Dec 21, 2011 at 8:15
  • this is the content that i copied from the developer tool of chrome. The JS that i use is : JSON.stringify(parametersCollection)', var parametersCollection = [];` and this parametersCollection is an array of the following object function QueryParameters(paramName, paramValue) { this.ParamName = paramName; this.ParamValue = paramValue; } Commented Dec 21, 2011 at 9:02

1 Answer 1

1

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.

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

3 Comments

My JSON is similar to what you have given. And, how do i get the json in the above line to the controller.
Also is there no built-in mechanism to get this data in the correct format to the controller (built-in model binding)?
The use of JSON.stringify alongwith the json2.js solved the problem.

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.