如何用密钥反序列化json包含“。”在C#?

时间:2018-06-01 06:52:20

标签: c# json

我想创建类以反序列化JSON。我尝试使用http://json2csharp.com/,但它不起作用。

JSON

{
    "usertoken": "TqasdfgdSz0UleD-hdST5sigIE1MdOlKQ2HXrcvTwtCpveN9Fm",
    "expires_sec": 12095,
    "user": "testuser.com",
    ".expired": "Sat, 2 Jun 2018 09:25:00 GMT"
}

尝试使用以下课程但在.expired班级成员

中发出问题
public class Authentication
{
    public string usertoken { get; set; }
    public int expires_sec { get; set; }
    public string user { get; set; }
    public string .expired { get; set; }    //.will not work here
}

请建议

1 个答案:

答案 0 :(得分:4)

你走在正确的轨道上。您需要使用的只是JsonProperty。它将帮助您定义程序将使用的属性名称,并命名您的json字符串将在序列化和反序列化操作期间使用。

检查以下样本。

public class Authentication
{
    public string usertoken { get; set; }
    public int expires_sec { get; set; }
    public string user { get; set; }
    [JsonProperty (".expired")]
    public string expired { get; set; }
}

您需要安装 Newtonsoft.Json nuget包才能实现此目的。