如何转换JSON对象为自定义C#的对象吗?对象、自定义、JSON

2023-09-02 11:29:40 作者:北盼.

有没有一种简单的方法来填充我的C#对象通过AJAX传递JSON对象?

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

  {
    用户 : {
                 名:ASDF,
                 teamname:b的,
                 电子邮件:C,
                 玩家:1,
                              2]
             }
}
 

// C#WebMetod接收JSON对象

  [WebMethod的]
公共静态无效SaveTeam(目标用户)
{

}
 

// C#类,再presents传递给WebMethod的JSON对象的对象结构

 公共类用户
{
    公共字符串名称{;组; }
    公共字符串teamname {获得;组; }
    公共字符串电子邮件{获得;组; }
    公共阵列玩家{获得;组; }
}
 
js中的json对象的属性怎么取值

解决方案

使用JSON在C#中的一个好方法是 JSON。 NET

快速启动和放大器;从 JSON.NET API文档 - 官方网站帮助您使用它。

如何使用它的一个例子:

 公共类用户
{
    公众用户(JSON字符串)
    {
        JObject jObject = JObject.Parse(JSON);
        JToken与juser = jObject [用户];
        名称=(字符串)与juser [名称];
        teamname =(字符串)与juser [teamname];
        电子邮件=(字符串)与juser [电子邮件];
        玩家与juser = [玩家]的ToArray()。
    }

    公共字符串名称{;组; }
    公共字符串teamname {获得;组; }
    公共字符串电子邮件{获得;组; }
    公共阵列玩家{获得;组; }
}

// 使用
私人无效的run()
{
    字符串JSON = @{用户:{名:ASDF,teamname:B,电子邮件:C, 玩家:[1,2]}};
    用户的用户=新用户(JSON);

    Console.WriteLine(姓名:+ user.name);
    Console.WriteLine(Teamname:+ user.teamname);
    Console.WriteLine(电子邮件:+ user.email);
    Console.WriteLine(玩家:);

    的foreach(VAR球员user.players)
        Console.WriteLine(运动员);
 }
 

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; }
}

解决方案

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);
 }

 
精彩推荐
图片推荐