是否有一个简单的方法来填充我的c#对象与JSON对象通过AJAX传递?

这是使用JSON.stringify从页面传递给c# WEBMETHOD的JSON对象

{
    "user": {
        "name": "asdf",
        "teamname": "b",
        "email": "c",
        "players": ["1", "2"]
    }
}

接收JSON对象的c# webmethod

[WebMethod]
public static void SaveTeam(Object user)
{

}

c#类,表示传递给WebMethod的JSON对象的对象结构

public class User
{
    public string name { get; set; }
    public string teamname { get; set; }
    public string email { get; set; }
    public Array players { get; set; }
}

当前回答

codeplex上的JSON c#类生成器生成的类可以很好地与NewtonSoftJS一起工作。

其他回答

而不是仅仅作为一个对象发送。

创建一个可访问的公共属性类,并将数据发送给Webmethod。

[WebMethod]
public static void SaveTeam(useSomeClassHere user)
{
}

在ajax调用中使用相同的参数名称来发送数据。

codeplex上的JSON c#类生成器生成的类可以很好地与NewtonSoftJS一起工作。

另一个非常简单的解决方案是使用Newtonsoft库。Json:

User user = JsonConvert.DeserializeObject<User>(jsonString);
public static class Utilities
{
    public static T Deserialize<T>(string jsonString)
    {
        using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString)))
        {    
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
            return (T)serializer.ReadObject(ms);
        }
    }
}

更多信息请访问以下链接 http://ishareidea.blogspot.in/2012/05/json-conversion.html

关于DataContractJsonSerializer类你可以在这里阅读。

在性能方面,我发现ServiceStack的序列化器比其他的快一些。它是ServiceStack中的JsonSerializer类。文本名称空间。

https://github.com/ServiceStack/ServiceStack.Text

ServiceStack通过NuGet包提供: https://www.nuget.org/packages/ServiceStack/