2017-12-24 9 views
0

Ich versuche Top 5 profitabelsten Münzen für den Bergbau von WhatToMine mit ihren JSON in meinem Haustier C# -Projekt zu bekommen.WhatToMine Json Coins Objekt zu Liste

Das Problem besteht darin, dass anstelle eines Arrays dieser Website gibt nur einzelne Objekt (ich die Liste der Eigenschaften der Kürze halber sortiert haben):

{ 
    "coins": 
    { 
     "Hush": 
     { 
     "id":168, 
     "tag":"HUSH", 
     "algorithm":"Equihash", 
     }, 
     "Zclassic": 
     { 
     "id":167, 
     "tag":"ZCL", 
     "algorithm":"Equihash" 
     } 
} 

ich nicht wirklich Münze Namen benötigen, als Tag ist ausreichend, so würde ich mag so etwas haben:

[ 
    { 
     "id":168, 
     "tag":"HUSH", 
     "algorithm":"Equihash", 
    }, 
    { 
     "id":167, 
     "tag":"ZCL", 
     "algorithm":"Equihash" 
    } 
] 

ich versuchte JSON2CSharp zu verwenden, aber es erzeugt für jede Münze eine Reihe von Klassen mit den gleichen Eigenschaften ein. Und weil ständig neue hinzugefügt werden, möchte ich nicht jedes Mal meinen Code ändern.

Sicher kann ich einige suchen/ersetzen oder regex, um die JSON-Antwort-String aussehen wie ich brauche, aber ich denke, wahre Entwickler (denen ich nicht Teil bin) wissen eine bessere und elegantere Art der Deserialisierung eines einzigen Objekt in Liste/Array.

+1

Eine der Antworten hier funktioniert: https://stackoverflow.com/questions/4535840/deserialize-json-object-into-dynamic-object- using-json-net – rene

+1

Der Klassenname spielt keine Rolle, Sie könnten also etwas wie 'CoinItem' verwenden, das als' {id, tag, algo} 'definiert ist und alle/alle Typen zuordnen (' CoinItem Hush ... ') – Plutonix

+0

Thx Leute, ich habe mir die Dokumente und Renes Link angesehen, aber ich kann es nicht zur Arbeit bringen. –

Antwort

3

Sofern Sie nicht einen sehr speziellen Anwendungsfall haben, empfehle ich die Verwendung Newtonsoft.Json als Ihre JSON-Bibliothek de-facto. Es wird Ihnen viel Ärger ersparen.

Das Problem ist, dass diese Seite anstelle von Array

Kartenobjekte zu Dictionary nur einzelnes Objekt zurück.

Bevorzugen automatische Serialisierung/Deserialisierung wenn möglich:

using System.Collections.Generic; 
using System.Net; 

using Newtonsoft.Json; 

namespace WhatToMine 
{ 
    using MineBlob = Dictionary<string, Dictionary<string, CoinBlob>>; 

    class CoinBlob 
    { 
     [JsonProperty(PropertyName = "id")] 
     public int Id; 
     [JsonProperty(PropertyName = "tag")] 
     public string Tag; 
     [JsonProperty(PropertyName = "algorithm")] 
     public string Algorithm; 
     [JsonProperty(PropertyName = "block_time")] 
     public double BlockTime; 
     [JsonProperty(PropertyName = "block_reward")] 
     public double BlockReward; 
     [JsonProperty(PropertyName = "block_reward24")] 
     public double BlockReward24; 
     [JsonProperty(PropertyName = "last_block")] 
     public long LastBlock; 
     [JsonProperty(PropertyName = "difficulty")] 
     public double Difficulty; 
     [JsonProperty(PropertyName = "difficulty24")] 
     public double Difficulty24; 
     [JsonProperty(PropertyName = "nethash")] 
     public long NetHash; 
     [JsonProperty(PropertyName = "exchange_rate")] 
     public double ExchangeRate; 
     [JsonProperty(PropertyName = "exchange_rate24")] 
     public double ExchangeRate24; 
     [JsonProperty(PropertyName = "exchange_rate_vol")] 
     public double ExchangeRateVolume; 
     [JsonProperty(PropertyName = "exchange_rate_curr")] 
     public string ExchangeRateCurrency; 
     [JsonProperty(PropertyName = "market_cap")] 
     public string MarketCapUsd; 
     [JsonProperty(PropertyName = "estimated_rewards")] 
     public string EstimatedRewards; 
     [JsonProperty(PropertyName = "estimated_rewards24")] 
     public string EstimatedRewards24; 
     [JsonProperty(PropertyName = "btc_revenue")] 
     public string BtcRevenue; 
     [JsonProperty(PropertyName = "btc_revenue24")] 
     public string BtcRevenue24; 
     [JsonProperty(PropertyName = "profitability")] 
     public double Profitability; 
     [JsonProperty(PropertyName = "profitability24")] 
     public double Profitability24; 
     [JsonProperty(PropertyName = "lagging")] 
     public bool IsLagging; 
     [JsonProperty(PropertyName = "timestamp")] 
     public long TimeStamp; 
    } 

    class Program 
    { 
     const string JsonUrl = "http://whattomine.com/coins.json"; 

     static void Main(string[] args) 
     { 
      using (var client = new WebClient()) { 
       var json = client.DownloadString(JsonUrl); 
       var blob = JsonConvert.DeserializeObject<MineBlob>(json); 
       // Do something with the data blob... 
      } 
     } 
    } 
} 
+0

Thx, Koby! Das ist was ich suche und ja, ich benutze NetonSoft JSON. Um also alle Münzen in der Wörterbuchwert-Sammlung zu haben, graben Sie einfach in die Wörterbuch-Werte ein: var blob = JsonConvert.DeserializeObject (json) .Values.ElementAt (0) .Values; –