【问题标题】:Write Custom c# class to represent the JSON?编写自定义 c# 类来表示 JSON?
【发布时间】:2014-10-22 14:54:56
【问题描述】:

我有一个 JSON 字符串,我需要为其创建 C# 类,然后以类似的格式解析整个 List。 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; }
    }

【问题讨论】:

    标签: asp.net json c#-4.0 serialization json.net


    【解决方案1】:

    可能是部分 JSON 数据 无法转换为 UserData 属性

    马上,您可以看到"DT_RowId": "row_20" 无法转换为long UserId

    在转换之外使用 try catch 块,并查看异常。

    例如,

    private string Json
    {
        get { 
            return @"
            {
                ""draw"": 4,
                ...
            }"; 
        }
    }
    
    try
    {
        JsonValdate result = JsonConvert.DeserializeObject<JsonValdate>(Json);
    }
    catch (Exception ex)
    {
         // Debug
    }
    

    这是我的测试方法

    由于转换不起作用,请不要一次放置所有字段。

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

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

    【讨论】:

    • 我已将长的UserId转换为字符串,而Json转换。它工作正常。我在数据数组索引中没有得到“0”和“1”。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多