Home Deserializing Jsonobject in mvc webapi
Reply: 0

Deserializing Jsonobject in mvc webapi

user1393 Published in June 24, 2018, 3:09 am

Im trying to send a jSon object to a WebApiController and deserialize it to an object but it just gives me an empty object with zero and null.

Id much rather do model binding though but that does not seem to work in Webappi like it does with AJAX call to a normal MVC controller ?

Javascript (jQuery)

var data = {
                    siffra : 23,
                    text : "Meddelande från andra sidan!"


                    url: "api/Stuff/PostStuff",
                    method: "POST",
                    data: JSON.stringify(data),
                    success: function (result) {
                        console.log("SUCESS: " + result);
                    error: function (data) {
                        console.log("error: " + data.responseText);

WebApiController method

 public string PostStuff(JObject jsonObject)
        Stuff stuff  = JsonConvert.DeserializeObject<Stuff>(jsonObject.ToString());
        return "thanks";


    public class Stuff
        public int siffra;
        public string text;     

JObject before deserialization: {"{\"siffra\":23,\"text\":\"Meddelande från andra sidan!\"}": ""}

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.480577 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO