Asp.net 编写自定义c#类来表示JSON?

Asp.net 编写自定义c#类来表示JSON?,asp.net,json,c#-4.0,serialization,json.net,Asp.net,Json,C# 4.0,Serialization,Json.net,我有一个JSON字符串,需要为其创建C#类,然后以类似的格式解析整个列表。JSON字符串包含“0”和“1”。我已经用 [JsonProperty(“0”)] 但看起来它不起作用了 { "draw": 4, "recordsTotal": 57, "recordsFiltered": 57, "data": [ { "0": "Charde", "1": "Marshall", "2": "Regional Director",

我有一个JSON字符串,需要为其创建C#类,然后以类似的格式解析整个列表。JSON字符串包含“0”和“1”。我已经用

[JsonProperty(“0”)]

但看起来它不起作用了

 {
  "draw": 4,
  "recordsTotal": 57,
  "recordsFiltered": 57,
  "data": [
    {
      "0": "Charde",
      "1": "Marshall",
      "2": "Regional Director",
      "3": "San Francisco",
      "4": "16th Oct 08",
      "5": "$470,600",
      "DT_RowId": "row_13"
    },
    {
      "0": "Colleen",
      "1": "Hurst",
      "2": "Javascript Developer",
      "3": "San Francisco",
      "4": "15th Sep 09",
      "5": "$205,500",
      "DT_RowId": "row_9"
    },
    {
      "0": "Dai",
      "1": "Rios",
      "2": "Personnel Lead",
      "3": "Edinburgh",
      "4": "26th Sep 12",
      "5": "$217,500",
      "DT_RowId": "row_20"
    }]
    }
类,我已尝试使用该JSON

public class UserData
    {
        [JsonProperty("0")]
        public string Name { get; set; }

        [JsonProperty("1")]
        public string Email { get; set; }

       //Having more JSON properites 

        [JsonProperty("DT_RowId")]
        public long UserId { get; set; }
    }

    public class JsonValdate
    {
       public string draw { get; set; }
       public int length { get; set; }
       public int start { get; set; }
       public int recordsFiltered { get; set; }
       public int recordsTotal { get; set; }

       [JsonProperty("data")]
       public UserData[] data { get; set; }
    }

可能是一些JSON数据无法转换为用户数据属性

马上,您可以看到
“DT_RowId”:“row_20”
无法转换为
长用户ID

在转换之外使用尝试catch block,并查看异常

比如说,

private string Json
{
    get { 
        return @"
        {
            ""draw"": 4,
            ...
        }"; 
    }
}

try
{
    JsonValdate result = JsonConvert.DeserializeObject<JsonValdate>(Json);
}
catch (Exception ex)
{
     // Debug
}
私有字符串Json
{
获取{
返回@”
{
“绘制”:4,
...
}"; 
}
}
尝试
{
JsonValdate结果=JsonConvert.DeserializeObject(Json);
}
捕获(例外情况除外)
{
//调试
}
下面是我的测试方法 由于转换不起作用,请不要同时放置所有字段

从以下工作字段开始。然后一次添加一个字段

private string Json
{
    get { return @"
        {
        ""draw"": 4,
        ""recordsTotal"": 57,
        ""recordsFiltered"": 57,
        ""data"": [
                {
                    ""0"": ""Charde"",
                    ""1"": ""Marshall""
                },
                {
                    ""0"": ""Colleen"",
                    ""1"": ""Hurst""
                },
                {
                    ""0"": ""Dai"",
                    ""1"": ""Rios""
                }]
        }"; 
    }
}

public class UserData
{
    [JsonProperty("0")]
    public string Name { get; set; }

    [JsonProperty("1")]
    public string Email { get; set; }
}

public class JsonValdate
{
    public string draw { get; set; }
    public int length { get; set; }
    public int start { get; set; }
    public int recordsFiltered { get; set; }
    public int recordsTotal { get; set; }

    [JsonProperty("data")]
    public UserData[] data { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        JsonValdate result = JsonConvert.DeserializeObject<JsonValdate>(Json);
    }
    catch (Exception ex)
    {

    }
}
私有字符串Json
{
获取{return@“
{
“绘制”:4,
“recordsTotal”:57,
“已过滤的记录”:57,
“数据”:[
{
“0”:“Charde”,
“1”:“马歇尔”
},
{
“0”:“Colleen”,
“1”:“赫斯特”
},
{
“0”:“戴”,
“1”:“Rios”
}]
}"; 
}
}
公共类用户数据
{
[JsonProperty(“0”)]
公共字符串名称{get;set;}
[JsonProperty(“1”)]
公共字符串电子邮件{get;set;}
}
公共类JsonValdate
{
公共字符串draw{get;set;}
公共整数长度{get;set;}
public int start{get;set;}
公共int记录过滤{get;set;}
public int recordsTotal{get;set;}
[JsonProperty(“数据”)]
公共用户数据[]数据{get;set;}
}
受保护的无效页面加载(对象发送方、事件参数e)
{
尝试
{
JsonValdate结果=JsonConvert.DeserializeObject(Json);
}
捕获(例外情况除外)
{
}
}

我已将长格式的用户ID转换为字符串,而Json转换。它工作得很好。我在数据数组索引中没有得到“0”和“1”。谢谢