2016-04-12 18 views
0

banka tatil tarihleri ​​almak için bir API erişen bir ASP MVC projesi var: https://www.gov.uk/bank-holidays.jsonJSON Ayrıştırma Resmi Tatil Takvimi

ben seviyesine inmek çalıştığınızda ancak sorunları alıyorum JSON ayrıştırmak zaman olaylar'. Şu anda var:

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

Anlamlı şey bu deserializer son nesne artığını edebilmek için görünmüyor olabilir. Çeşitli nesneler, listeler, diziler vb. Denedim ama hiçbir şey işe yaramıyor gibi görünüyor. Sadece nesnelere dökebiliyorum gibi görünüyor.

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 
} 

Bu oldukça basit olacağını düşündüm ama her şeyi denedim:

İdeal JSON gibi anlamlı bir nesneye ayrıştırılması istiyorum. Eminim ki eksik olduğum bir şey.

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

Sonra bu şekilde onu deserialise:

JavaScriptSerializer daha iyi bir seçim kullanmayın

Teşekkür

+1

Kullanımdan kaldırılmış JavaScriptSerializer'ı KULLANMAYIN! Web API bile Json.NET kullanır. JSon standart hale getirilmeden önce JavaScriptSerializer kuruldu. Ayrıca, neden ülkeler, bölümler, olaylar gibi somut sınıflar yerine sözlüklere niçin kaçıyorsunuz? –

+0

json api yerine başka bir yol nager.date nuget paketi kullanıyoruz, ayrıca destek için uk [nager.date] (https://www.nuget.org/packages/Nager.Date) – live2

cevap

4

Eğer JSON'dan sınıfları üretebilir json2csharp.com sitesinde JSON.NET

olduğunu

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

EDIT RootObject, kötü adlar için el ile ilgili özellikler eklendi.

{ 
    "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

bu harika, teşekkürler . Kötü adlandırma kuralıyla nasıl başa çıkılacağı hakkında bir fikrin var mı? Basit bir dize yerine json dizesini api – GrahamJRoy

+1

'dan geri döndüğümde değiştirdim. Bunu yapmak için 'RootObject' değiştirdim. – BWA

0

Ben ölçüde json.net lirary

Burada anahtar problème Tür çözünürlükle tipini (bankholiday) kullanmak gerektiğini düşünüyorum kullanmak için teşvik edecek: basitleştirilmiş JSON üzerinde test:

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