c# - Parse incoming JSON (from javascript Ajax post) using Web API controller -
i have 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 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 , jobj1 used testing. jsonstring1 filled values. v2 filled values @ runtime.
however have 4 methods
- equals()
- gethashcode()
- gettype()
- tostring()
available when typing code...
i have cast values in jsonstring1 collection of type reservation or how can loop or use linq, when try values don't populated(everything null or 0).
example, below can loop , use linq values not populated(are 0 , 0 instead of 70656 , 1 ):
var parsedvar = json_serializer.deserialize<icollection<reservationmodelforjson>>(jsonstring1); var clubid = parsedvar.first().clubid;//is 0 should 70656 var courtnumber = parsedvar.first().courtnumber;//is 0 should 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 class below
public class jsonbinder { public reservationmodelforjson reservation { get; set; } } and changed controller
public string post(ienumerable<jsonbinder> reservations) { int clubid = reservations.first().reservation.clubid; int courtnumber = reservations.first().reservation.courtnumber; return "tnkx!"; }
this because deserialization failing. trying deserialize on server side doesn't match json model. trying deserialize list of objects property name reservation(which object expected properties). fix, remodel json 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 class object property name reservation has expected properties, ticketid, clubid, etc...
Comments
Post a Comment