2017-07-24 270 views
2

我正在用cryptocurrencies創建一些應用程序,並且我在某處發現了API的問題。簡化JSON對象以反序列化

我需要一些提示如何簡化我的類的序列化。

public class Data 
{ 
    public SUB SUB { get; set; } 
    public USC USC { get; set; } 
    public DUX DUX { get; set; } 
    public XPS XPS { get; set; } 
    public EQT EQT { get; set; } 
    ... //and a lot more of same classes 

} 

Here is that REST page with JSON

我已經使用http://json2csharp.com/類生成 - 但在那之後我留下了數以百計的看着相同的類 - 只有其他名稱。我試過替換它,但總是留有空值。

現在我與: -

public class Data 
    { 
     public string Id { get; set; } 
     public string Url { get; set; } 
     public string ImageUrl { get; set; } 
     public string Name { get; set; } 
    ... 
    } 
    public class RootObject 
    { 
     public string BaseLinkUrl { get; set; } 
     public List<List<Data>> Data { get; set; } 
     public int Type { get; set; } 
    } 


    public static async Task<T> DeserializeStringToObject<T>(string url) 
    { 
     return JsonConvert 
      .DeserializeObject<T>(await GetStreamFromUr(url)); 
    } 

或者,也許我應該使用不同的解串器?或者每次迭代循環時檢查一個對象?

+2

使用字典解串器。在你的'RootObject'中添加一個'public Dictionary Data'成員,然後爲每個加密貨幣的固定屬性創建一個'Data'類型。有關詳細信息,請參見[使用ID作爲名稱從json對象創建一個強類型的c#對象](https://stackoverflow.com/q/34213566/3744182)。 – dbc

回答

1

你在那裏有一本字典。改爲嘗試這些類。

public class RootObject 
{ 
    public string Response { get; set; } 
    public string Message { get; set; } 
    public string BaseImageUrl { get; set; } 
    public string BaseLinkUrl { get; set; } 
    public Dictionary<string, CurrencyDefinition> Data { get; set; } 
    public int Type { get; set; } 
} 

public class CurrencyDefinition 
{ 
    public string Id { get; set; } 
    public string Url { get; set; } 
    public string ImageUrl { get; set; } 
    public string Name { get; set; } 
    public string CoinName { get; set; } 
    public string FullName { get; set; } 
    public string Algorithm { get; set; } 
    public string ProofType { get; set; } 
    public string FullyPremined { get; set; } 
    public string TotalCoinSupply { get; set; } 
    public string PreMinedValue { get; set; } 
    public string TotalCoinsFreeFloat { get; set; } 
    public string SortOrder { get; set; } 
} 
2

我做一個小測試,使用RestSharp和它運作良好

namespace ConsoleApp1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var client = new RestClient("https://www.cryptocompare.com/api"); 

      var response = client.Execute<DataContainer>(new RestRequest("/data/coinlist")); 

      var data = response.Data;  } 
    } 

    public class DataContainer 
    { 
     public string Message { get; set; } 
     public Dictionary<string, DataItem> Data { get; set; } 
    } 

    public class DataItem 
    { 
     public string Id { get; set; } 
     public string Url { get; set; } 
     public string ImageUrl { get; set; } 
     public string Name { get; set; } 

    } 
} 

基本上我更改數據屬性的Dictonary類型。這樣它將序列化字典中的所有數據,你可以像這樣使用

data.Data["SUB"].Id 
-1

嘗試與newtonsoft

enter link description here

+0

歡迎使用stackoverflow。感謝您發佈此信息,但它沒有回答這個問題。 OP已經在使用newtonsoft,並且正在考慮如何設計他們的數據模型來輕鬆地反序列化示例JSON。此外,它基本上是一個鏈接唯一的答案,這是[灰心](https://meta.stackexchange.com/a/8259/344280)。看看[我如何寫出一個好的答案?](https://stackoverflow.com/help/how-to-answer)它解釋了什麼,具體來說,這個問題是要求什麼?確保你的答案提供*和*簡潔是可以接受的,但更全面的解釋更好。* – dbc