我的Web API C#.net服务器无法反序列化JSON对象

[英]my Web API C#.net server is not able to Deserialize JSON Object


This my client code

这是我的客户端代码

var sayeed = { firstname: "Sayeed", surname: "Alquiaty" };
alert(JSON.stringify({ person: sayeed }));
$.ajax({
    url: "api/parent",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify({ person: sayeed }),
    success: function(response) {
        response ? alert("It worked!") : alert("It didn't work.");
    }
});

At the Server side

在服务器端

public class Person {
    public string Firstname { get; set; }
    public string Surname { get; set; }
}

//Here I am able to receive object but is is not FirstName = null and Surname = null
// POST api/parent
public bool PostParent(Person person) {
    return person != null;
}

So the client get a success message but actually JSON Object is not Deserialize

因此客户端获得成功消息,但实际上JSON对象不是反序列化

I have tried other method like using JObject but that is currupting the Json object, what i mean is it is converting the client object as Key and the then adding : "". Which is not what i want.

我尝试过其他方法,比如使用JObject但是这会破坏Json对象,我的意思是它将客户端对象转换为Key,然后添加:“”。这不是我想要的。

3 个解决方案

#1


There are several problems in your code:

您的代码中存在几个问题:

  1. The deserialization is case sensitive, so the properties on JS side must have the same case that the properties on the server side
  2. 反序列化区分大小写,因此JS端的属性必须与服务器端的属性具有相同的大小写

  3. You need to pass the person object as parameter directly, not included as the person property of an object
  4. 您需要直接将person对象作为参数传递,而不是作为对象的person属性包含

  5. although it generally doesn't hurt, you do't need to stringify the object. jQuery ajax will do it automatically for you
  6. 虽然它通常不会受到伤害,但您不需要对对象进行字符串化。 jQuery ajax会自动为你做

In regards to 1, you can setup the Web API serializer to convert server side PascalCase to client side camelCase, and viceversa, but you still need to take this into account when converting the property names between client and server side:

关于1,您可以设置Web API序列化程序将服务器端PascalCase转换为客户端camelCase,反之亦然,但在客户端和服务器端之间转换属性名称时仍需要考虑这一点:

var jsonformatter
  = GlobalConfiguration.Configuration.Formatters.JsonFormatter;

jsonformatter.SerializerSettings.ContractResolver
  = new CamelCasePropertyNamesContractResolver();

#2


Thanks you all for trying to help me resolve this issue. But I could discover the bug in my server side code it is this

谢谢大家帮我解决这个问题。但我可以发现我的服务器端代码中的错误就是这个

config.Formatters.JsonFormatter.UseDataContractJsonSerializer = true;
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.XmlFormatter.UseXmlSerializer = false;

So now I commented the above config setting and every thing started working perfectly. But this took me 2 days to really figure out the real cause(supposedly)

所以现在我评论了上面的配置设置,每件事情都开始完美。但这花了我2天才真正找出真正的原因(据说)

#3


Sorry I forgot to mention that I have made small change to PostParent(JObject result) so the result is able to receive the JSON object deserialized correct. Then I can create a person object from it. Hope this is helpfull

抱歉,我忘了提到我对PostParent(JObject结果)做了一些小改动,所以结果能够接收反序列化正确的JSON对象。然后我可以从中创建一个人物。希望这是有帮助的

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/05/03/54473ed34a17ab815c5cd718d59a4a27.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告