JSON解析银行假日日历

Gra*_*Roy 1 c# json

我有一个ASP MVC项目正在访问API以获取银行假日日期:https://www.gov.uk/bank-holidays.json

当我解析JSON时,当我试图达到"事件"的水平时,我遇到了问题.目前我有:

var json = new WebClient().DownloadString("https://www.gov.uk/bank-holidays.json");
JavaScriptSerializer serializer = new JavaScriptSerializer();
var test = serializer.Deserialize<Dictionary<string, Dictionary<string, object>>>(json);
Run Code Online (Sandbox Code Playgroud)

我似乎无法将这个反序列化器中的最终对象转换为任何有意义的东西.我尝试了各种对象,列表,数组等,但似乎没有任何工作.我似乎只能把它投射到物体上.

理想情况下,我希望将JSON解析为有意义的对象,例如:

public class BankHoliday
{
  public DateTime Date { get; set; }
  public string Title { get; set; }
  public Country CountryCode { get; set; }
  public string Notes { get; set; }
  public bool Bunting { get; set; }
}

public enum Country
{
  EnglandWales,
  Scotland,
  NorthernIreland
}
Run Code Online (Sandbox Code Playgroud)

我觉得这很简单,但我已经尝试了一切.我确信这是我想念的简单事.

谢谢

BWA*_*BWA 5

不要使用JavaScriptSerializer更好的选择是JSON.NET

在网站json2csharp.com上,您可以从JSON生成类:

public class Event
{
    public string title { get; set; }
    public string date { get; set; }
    public string notes { get; set; }
    public bool bunting { get; set; }
}

public class EnglandAndWales
{
    public string division { get; set; }
    public List<Event> events { get; set; }
}

public class Scotland
{
    public string division { get; set; }
    public List<Event> events { get; set; }
}

public class NorthernIreland
{
    public string division { get; set; }
    public List<Event> events { get; set; }
}

public class RootObject
{
    [JsonProperty(PropertyName = "england-and-wales")]
    public EnglandAndWales EnglandAndWales { get; set; }
    public Scotland scotland { get; set; }
    [JsonProperty(PropertyName = "northern-ireland")]
    public NorthernIreland NorthernIreland { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

然后用这种方式对其进行反序列化:

RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(output);
Run Code Online (Sandbox Code Playgroud)

编辑 添加属性RootObject以解决错误名称.在简化的JSON上测试:

{
    "england-and-wales": {
        "division": "england-and-wales",
        "events": [{
            "title": "New Year’s Day",
            "date": "2012-01-02",
            "notes": "Substitute day",
            "bunting": true
        },
        {
            "title": "Good Friday",
            "date": "2012-04-06",
            "notes": "",
            "bunting": false
        },          
        {
            "title": "Boxing Day",
            "date": "2017-12-26",
            "notes": "",
            "bunting": true
        }]
    },
    "scotland": {
        "division": "scotland",
        "events": [{
            "title": "2nd January",
            "date": "2012-01-02",
            "notes": "",
            "bunting": true
        },
        {
            "title": "New Year’s Day",
            "date": "2012-01-03",
            "notes": "Substitute day",
            "bunting": true
        },                  
        {
            "title": "Boxing Day",
            "date": "2017-12-26",
            "notes": "",
            "bunting": true
        }]
    },
    "northern-ireland": {
        "division": "northern-ireland",
        "events": [{
            "title": "New Year’s Day",
            "date": "2012-01-02",
            "notes": "Substitute day",
            "bunting": true
        },
        {
            "title": "St Patrick’s Day",
            "date": "2012-03-19",
            "notes": "Substitute day",
            "bunting": true
        },      
        {
            "title": "Boxing Day",
            "date": "2017-12-26",
            "notes": "",
            "bunting": true
        }]
    }
}
Run Code Online (Sandbox Code Playgroud)