How to Convert JSON object to Custom C# object?

2018-12-31 04:35发布

Is there an easy way to populate my C# Object with the JSON object passed via AJAX?

//This is the JSON Object passed to C# WEBMETHOD from the page using JSON.stringify

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

//C# WebMetod That receives the JSON Object

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

}

//C# Class that represents the object structure of JSON Object passed in to the WebMethod

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

13条回答
回忆,回不去的记忆
2楼-- · 2018-12-31 05:08

Another Really simple Solution is using the library Newtonsoft.Json:

User user = JsonConvert.DeserializeObject<User>(jsonString);
查看更多
后来的你喜欢了谁
3楼-- · 2018-12-31 05:11

Given your code sample, you shouldn't need to do anything else.

If you pass that JSON string to your web method, it will automatically parse the JSON string and create a populated User object as the parameter for your SaveTeam method.

Generally though, you can use the JavascriptSerializer class as below, or for more flexibility, use any of the various Json frameworks out there (Jayrock JSON is a good one) for easy JSON manipulation.

 JavaScriptSerializer jss= new JavaScriptSerializer();
 User user = jss.Deserialize<User>(jsonResponse); 
查看更多
几人难应
4楼-- · 2018-12-31 05:14

A good way to use JSON in C# is with JSON.NET

Quick Starts & API Documentation from JSON.NET - Official site help you work with it.

An example of how to use it:

public class User
{
    public User(string json)
    {
        JObject jObject = JObject.Parse(json);
        JToken jUser = jObject["user"];
        name = (string) jUser["name"];
        teamname = (string) jUser["teamname"];
        email = (string) jUser["email"];
        players = jUser["players"].ToArray();
    }

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

// Use
private void Run()
{
    string json = @"{""user"":{""name"":""asdf"",""teamname"":""b"",""email"":""c"",""players"":[""1"",""2""]}}";
    User user = new User(json);

    Console.WriteLine("Name : " + user.name);
    Console.WriteLine("Teamname : " + user.teamname);
    Console.WriteLine("Email : " + user.email);
    Console.WriteLine("Players:");

    foreach (var player in user.players)
        Console.WriteLine(player);
 }
查看更多
泪湿衣
5楼-- · 2018-12-31 05:14

Since we all love one liners code

Newtonsoft is faster than java script serializer. ... this one depends on the Newtonsoft NuGet package, which is popular and better than the default serializer.

if we have class then use below.

Mycustomclassname oMycustomclassname = Newtonsoft.Json.JsonConvert.DeserializeObject<Mycustomclassname>(jsonString);

no class then use dynamic

var oMycustomclassname = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(jsonString);
查看更多
柔情千种
6楼-- · 2018-12-31 05:18

JSON.Net is your best bet but, depending on the shape of the objects and whether there are circular dependencies, you could use JavaScriptSerializer or DataContractSerializer.

查看更多
裙下三千臣
7楼-- · 2018-12-31 05:19

The JSON C# class generator on codeplex generates classes which work well with NewtonSoftJS.

查看更多
登录 后发表回答