2016-05-19 167 views
1

我想將這個從API(OpenBank項目)返回的JSON轉換爲C#。但作爲一個新手,我一直有很多不必要的問題。JSON反序列化問題

{ 
    "banks": [ 
    { 
     "id": "rbs", 
     "short_name": "The Royal Bank of Scotland", 
     "full_name": "The Royal Bank of Scotland", 
     "logo": "http://www.red-bank-shoreditch.com/logo.gif", 
     "website": "http://www.red-bank-shoreditch.com" 
    }, 
    { 
     "id": "test-bank", 
     "short_name": "TB", 
     "full_name": "Test Bank", 
     "logo": null, 
     "website": null 
    }, 
    { 
     "id": "testowy_bank_id", 
     "short_name": "TB", 
     "full_name": "Testowy bank", 
     "logo": null, 
     "website": null 
    }, 
    { 
     "id": "nordea", 
     "short_name": "Nordea", 
     "full_name": "Nordea Bank AB", 
     "logo": "http://logonoid.com/images/nordea-logo.jpg", 
     "website": "http://www.nordea.com/" 
    }, 
    { 
     "id": "nordeaab", 
     "short_name": "Nordea", 
     "full_name": "Nordea Bank AB", 
     "logo": "http://logonoid.com/images/nordea-logo.jpg", 
     "website": "http://www.nordea.com/" 
    }, 
    { 
     "id": "hsbc-test", 
     "short_name": "HSBC Test", 
     "full_name": "Hongkong and Shanghai Bank", 
     "logo": null, 
     "website": null 
    }, 
    { 
     "id": "erste-test", 
     "short_name": "Erste Bank Test", 
     "full_name": "Erste Bank Test", 
     "logo": null, 
     "website": null 
    }, 
    { 
     "id": "deutche-test", 
     "short_name": "Deutche Bank Test", 
     "full_name": "Deutche Bank Test", 
     "logo": null, 
     "website": null 
    }, 
    { 
     "id": "obp-bankx-m", 
     "short_name": "Bank X", 
     "full_name": "The Bank of X", 
     "logo": "https://static.openbankproject.com/images/bankx/bankx_logo.png", 
     "website": "https://www.example.com" 
    } 
    ] 
} 

我已驗證JSON字符串,它似乎是正確的。

現在,這是我用來嘗試反序列化接收到的內容:

using (StreamReader reader = new StreamReader(response.GetResponseStream())) 
{ 
    string content = reader.ReadToEnd(); 
    bankslist info = JsonConvert.DeserializeObject<bankslist>(content); 
} 

這些類I'm使用(我用這個json2csharp):

public class bankslist 
{ 
    public List<banks> banklist { get; set; } 
} 

public class bankstuff 
{ 
    public banks banks; 
} 

public class banks 
{ 
    [JsonProperty(PropertyName = "id")] 
    public string id { get; set; } 

    [JsonProperty(PropertyName = "short_name")] 
    public string short_name { get; set; } 

    [JsonProperty(PropertyName = "full_name")] 
    public string full_name { get; set; } 

    [JsonProperty(PropertyName = "logo")] 
    public string logo { get; set; } 

    [JsonProperty(PropertyName = "website")] 
    public string website { get; set; } 
} 

似乎我沒有在銀行課上儲存任何我想要的信息。我試過但沒有任何資料顯示:

foreach (var item in info.banklist) 
{ 
    Debug.WriteLine("id=={0} .. full_name=={1} .. website=={2}", 
        item.id, item.full_name, item.website); 
} 

我在做什麼錯?

+0

歡迎堆棧溢出。如果其中一個答案適用於您,請點擊答案左邊的綠色複選標記將其標記爲已接受。當你獲得更多的聲譽時,你也可以提出你認爲有幫助的答案。 –

+0

感謝Brian的領導,仍然在這裏學習交易的技巧。 :) –

回答

1

你說你用json2csharp,但是當我把你的JSON到json2csharp我得到的以下內容:

public class Bank 
{ 
    public string id { get; set; } 
    public string short_name { get; set; } 
    public string full_name { get; set; } 
    public string logo { get; set; } 
    public string website { get; set; } 
} 

public class RootObject 
{ 
    public List<Bank> banks { get; set; } 
} 

然後您反序列化爲RootObject

string content = reader.ReadToEnd(); 
var info = JsonConvert.DeserializeObject<RootObject>(content); 

你可以看到它在這裏工作:https://dotnetfiddle.net/yTcnQh

+0

幫助一噸謝謝你的快速反應和簡單的決議,你放在這裏的鏈接將在未來幫助我很多。 –

0

您需要從banklist您bankslist類重命名銀行名單的銀行將JSON節點匹配:

public class bankslist 
{ 
    public List<banks> banks { get; set; } 
} 
+0

感謝人不知道這是正確的在我面前 –