Kip ei Kip ei - 2 months ago 14
JSON Question

Parse incoming JSON (from javascript 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 the 4 methods


  • Equals()

  • GetHashCode()

  • GetType()

  • ToString()



available when I am typing my code...

I have to cast the values in the
jsonString1
to a collection of type reservation or something some how which I can then loop or use linq, but when I try something like that the values don't get populated(everything is null or 0).

Example, below I can loop and use linq but the values are not populated(are 0 and 0 instead of 70656 and 1 ):

var parsedVar = json_serializer.Deserialize<ICollection<ReservationModelForJson>>(jsonString1);
var clubId = parsedVar.First().clubId;//is 0 should be 70656
var courtNumber = parsedVar.First().courtNumber;//is 0 should be 1


Anybody can solve this? Many thanks!

public class ReservationModelForJson
{
public string ticketId { get; set; }
public int clubId { get; set; }
public int courtNumber { get; set; }
public string crud_name { get; set; }
public string reservationtype_name { get; set; }
}


ANSWER/SOLUTION:

I added the class below


public class JsonBinder
{
public ReservationModelForJson reservation { get; set; }
}


And changed my controller into


public string Post(IEnumerable<JsonBinder> reservations)
{
int clubId = reservations.First().reservation.clubId;
int courtNumber = reservations.First().reservation.courtNumber;
return "tnkx!";
}

Answer

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...

Comments