JSON解析银行假日日历

时间:2016-04-12 08:54:25

标签: 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);

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

理想情况下,我希望将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
}

我认为这很简单,但我已经尝试了一切。我确信这是我想念的简单事。

由于

3 个答案:

答案 0 :(得分:5)

不要使用JavaScriptSerializer更好的选择json2csharp.com

在网站www.natashatarasova.de上,您可以从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; }
}

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

RootObject rootObject = JsonConvert.DeserializeObject<RootObject>(output);

修改 添加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
        }]
    }
}

答案 1 :(得分:0)

我非常鼓励你使用json.net lirary

我认为你关键问题是你需要在类型定义中使用你的类型(BankHoliday):

Dictionary<string, <Dictionary<string, BankHoliday>>

答案 2 :(得分:0)

为什么不只使用dynamic

package main

import (
    "fmt"
    "time"
)

func main() {
    layout := "01/02/2006 03:04 AM"
    if t, err := time.Parse(layout, "01/31/2000 12:59 AM"); err == nil {
        fmt.Println("Time decoded:", t)
    } else {
        fmt.Println("Failed to decode time:", err)
    }
}