2016-04-12 13 views
0

私が銀行休業日を取得するためのAPIにアクセスしているASP MVCプロジェクトを持っている:私はJSONをパースするとき、私はのレベルまで取得しようとすると、https://www.gov.uk/bank-holidays.jsonJSON解析バンクホリデーカレンダー

は、しかし、私は問題を取得していますそのイベント'。現在、私は持っています:

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 
} 

私はこれはかなり簡単だろうと思ったが、私はすべてを試してみました。私はそれが私が行方不明の単純なものだと確信しています。

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; } 
} 

そして、この方法でそれをdeserialise:

JavaScriptSerializerよりよい選択を使用しないでください

おかげ

+1

廃止予定のJavaScriptSerializerを使用しないでください。 Web APIでもJson.NETを使用します。 JavaScriptSerializerは、JSonが標準化される前に作成されました。また、なぜ国や部門、イベントなど具体的なクラスではなく、辞書に非直列化しようとしていますか? –

+0

json apiの代わりに、nager.date nugetパッケージを使用する以外は、uk [nager.date](https://www.nuget.org/packages/Nager.Date) – live2

答えて

4

あなたはJSONからクラスを生成することができます json2csharp.comサイトで JSON.NET

です

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

EDIT 悪い名前を手渡すために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 
     }] 
    } 
} 
+0

をサポートしています。ありがとう、ありがとう。悪い命名規則をどのように処理するかについてのアイデアはありますか?私は単純な文字列を行うことができます私は、JSONの文字列を戻ってから戻って – GrahamJRoy

+1

私はそれを処理する 'RootObject'を変更しました。 – BWA

0

私は非常に使用することをお勧めしますjson.net lirary

私はここであなたにキーproblèmeと思うあなたは型定義であなたのタイプ(BankHoliday)を使用する必要があるということです。

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