1

There are so many json serializing/deserializing options, so I'm not sure which one is the correct one to use, and why there are so many options to do seemingly the same thing. I've seen JsonConvert, JsonSerializer, JavaScriptSerializer, and several others.

Looking for help on correctly deserializng a json array into a c# list of complex objects.

JQUERY:

 var users = [];
    for (var i = 0; i < response.length; i++) 
    {
        var u = 
        {
            Id: response[i].id,
            UserName: response[i].username,
            FirstName: response[i].first_name,
            LastName: response[i].last_name    
        };
        users[i] = u;
    }

    var ul = JSON.stringify({ 'userlist': users});
    $.ajax({
        type: "POST",
        url: "/myurl/addusers",
        data: { 'userlist': ul },
        dataType: "json",
        success: function (response) {  

        },
        error: function (xhr, status, error) {

        }
    });

C# (this doesn't work):

        [HttpPost]
        public ActionResult AddUsers(string userlist)
        {
            JavaScriptSerializer ser = new JavaScriptSerializer();
            var users = ser.Deserialize<List<User>>(userlist); 
            ...
        }  

    [Serializable]
    public class User
    {
        public string Id { get; set; }
        public string UserName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
1
  • I'm not sure if/how it fits with MVC but .NET Web API already does all this functionality for you. You should look into making use of that. asp.net/web-api Commented Jul 11, 2013 at 16:05

2 Answers 2

3

Try this way:

var ul = JSON.stringify({ 'userlist': users});
    $.ajax({
        type: "POST",
        url: "/myurl/addusers",
        data: ul ,
        content-Type: 'application/json; charset=utf-8', 
        dataType: "json",
        success: function (response) {  

        },
        error: function (xhr, status, error) {

        }
    });

You already are setting userlist in the json ul so just asign it directly to data without wrapping it over userlist again, also set the contentType content-Type: 'application/json; charset=utf-8' in the ajax settings.

Also in your action just use:

    public ActionResult AddUsers(List<User> userlist)
    {
     //no need to do JavaScriptSerializer 
Sign up to request clarification or add additional context in comments.

3 Comments

This is actually what I had initially. I tried this again, however, I'm getting a count of 0 in my userlist when AddUsers gets called. Btw, thanks for pointing out the mistake in the jquery. I've corrected that.
@Prabhu Check in your network console (chrome) in the browser to see how the data goes out. Also add contentType: 'application/json; charset=utf-8', See my update
@Prabhu you are welcome. I did not notice that initially in your code, later i realized you had missed that.. :)
1

Change your action to accept List of users, the model binder will take care of the rest, like that:

 [HttpPost]
        public ActionResult AddUsers(List<User> users)
        {
            //use the 
        }

Change javascript to send the users array directly, without surrogate property:

var ul = JSON.stringify(users);
    $.ajax({
        type: "POST",
        url: "/myurl/addusers",
        data: ul ,
        dataType: "json",
        success: function (response) {  

        },
        error: function (xhr, status, error) {

        }
    });

1 Comment

Thanks. I tried this also (similar to PSL's method), but I'm still seeing a count of 0 items when AddUsers gets called, even though there is one item in ul.

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.