Kip ei February 2016

Parse incoming JSON (from javscript Ajax post) using Web API controller

I have the following script

<script>
    $(window).load(function () {
        var model = [
            {
                "reservation":
                {
                    "ticketId": "782274b8-10d9-444c-8f05-3117853e3ee0",
                    "clubId": 70656,
                    "courtNumber": 1,
                    "crud_name": "add",
                    "reservationtype_name": "toernooi"
                }
            }, 
            {
                "reservation":
                {
                    "ticketId": "782274b8-10d9-444c-8f05-3117853e3ee0",
                    "clubId": 70656,
                    "courtNumber": 1,
                    "crud_name": "update",
                    "reservationtype_name": "toernooi"
                }
            }
        ];
        alert(JSON.stringify(model));
        $.ajax({
            type: "POST",
            data: JSON.stringify(model),
            url: "http://localhost:59854/api/incomingapi",
            contentType: "application/json",
            dataType: 'json',
            success: function (result) { },
            error: function (errResult) { }
        });
    });
</script>

I have the following controller

public string Post(HttpRequestMessage request)
{
    string jsonString1 = request.Content.ReadAsStringAsync().Result;
    JavaScriptSerializer json_serializer = new JavaScriptSerializer();
    var v1 = json_serializer.DeserializeObject("{ \"test\":\"some data\" }");
    var v2 = json_serializer.DeserializeObject(jsonString1);
    JObject jobj1 = JObject.Parse("{ \"test\":\"some data\" }");
    JObject jobj2 = JObject.Parse(jsonString1);
    return "";
}

v1 and jobj1 are used for testing. The jsonString1 is filled with values. The v2 is filled with values at runtime.

However I only have t

Answers


nAviD February 2016

Try this :

json_serializer.Deserialize<List<ReservationModelForJson>>(jsonString1);

And your properties like courtNumber are in reservation object so for example you should use like parsedVar.First().courtNumber;. Structure of ReservationModelForJson matters in this case.


dhendrix February 2016

This should work. Instead of the HttpRequestMessage argument in your Post method, use a collection of ReservationModelForJson. Also, you will probably need to remove the JSON.stringify(model), and just pass the model.

public string Post(IEnumerable<ReservationModelForJson> reservations)
{
   //Now you can work with the reservations collection
   var firstRes = reservations.FirstOrDefault(); 
}


user2344183 February 2016

This is because your deserialization is failing. What your trying to deserialize on the server side doesn't match up with your json model. Currently your trying to deserialize a list of objects with property name reservation(which is a object with expected properties). To fix, remodel your json like below.

 var model = [
        {
                "ticketId": "782274b8-10d9-444c-8f05-3117853e3ee0",
                "clubId": 70656,
                "courtNumber": 1,
                "crud_name": "add",
                "reservationtype_name": "toernooi"
        }, 
        {
                "ticketId": "782274b8-10d9-444c-8f05-3117853e3ee0",
                "clubId": 70656,
                "courtNumber": 1,
                "crud_name": "update",
                "reservationtype_name": "toernooi"
        }
    ];

Or create a class with an object property name reservation that has expected properties, ticketId, clubId, etc...

Post Status

Asked in February 2016
Viewed 1,107 times
Voted 11
Answered 3 times

Search




Leave an answer