带有toString属性的Json有效负载

时间:2014-07-11 18:42:05

标签: json.net

当我尝试反序列化包含属性的toString名称的json时,我一直将属性设置为null

{   “field”:“status”,   “fieldtype”:“jira”,   “from”:“10000”,   “fromString”:“阻碍”,   “to”:“10006”,   “toString”:“评论” }

我尝试使用和不使用以下JsonProperty

public class ChangelogItem
{
    public string field { get; set; }
    public string fieldtype { get; set; }
    public string from { get; set; }
    public string fromString { get; set; }
    public string to { get; set; }

    //[JsonProperty(PropertyName = "toString")]
    //public string newString { get; set; }

    public string toString { get; set; }
}

但我一直得到一个空值。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

以下适用于Json.Net v6.0.3的工作正常:

public class ChangelogItem
{
    public string field { get; set; }
    public string fieldtype { get; set; }
    public string from { get; set; }
    public string fromString { get; set; }
    public string to { get; set; }
    public string toString { get; set; }
}

测试程序:

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        {
            ""field"": ""status"",
            ""fieldtype"": ""jira"",
            ""from"": ""10000"",
            ""fromString"": ""Impeded"",
            ""to"": ""10006"",
            ""toString"": ""Review""
        }";

        ChangelogItem item = JsonConvert.DeserializeObject<ChangelogItem>(json);
        Console.WriteLine(item.toString);
    }
}

输出:

Review