2017-12-24 4 views
0

내 애완 동물 C# 프로젝트에서 JSON을 사용하여 WhatToMine에서 마이닝 용으로 가장 수익성이 높은 동전 5 개를 얻으려고합니다. 태그이기 때문에, 정말 동전의 이름이 필요하지 않습니다WhatToMine Json 동전 대상 목록

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

:

문제는 배열 대신이 사이트는 단지 하나의 오브젝트 (I 간결 속성의 목록을 분류 한)을 반환한다는 것입니다

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

내가 JSON2CSharp을 사용하려고하지만 동일한 속성 각 동전 당 하나의 클래스의 무리를 생성 : 충분한, 그래서 이런 식으로 뭔가를하고 싶습니다. 새로운 코드가 계속 추가되기 때문에 매번 코드를 변경하고 싶지 않습니다.

분명히 내가 원하는 것처럼 JSON 응답 문자열을 보이게 만들려면 몇 가지 검색/바꾸기 또는 정규식을 수행 할 수 있지만 진정한 개발자 (일부는 아님)가 단일 직렬화를 더 좋고 더 우아한 방식으로 알고 있다고 생각합니다. list/array에 객체를 넣는다.

+1

하나는 작동합니다 :

가능한 자동 직렬화/직렬화 복원을 선호 https://stackoverflow.com/questions/4535840/deserialize-json-object-into-dynamic-object- using-json-net – rene

+1

클래스 이름은 중요하지 않으므로'{id, tag, algo}'로 정의 된'CoinItem'을 사용하고 임의/모든 타입을 매핑 할 수 있습니다 ('CoinItem Hush ...') – Plutonix

+0

Thx 너희들, 전에는 그 문서들과 르네의 링크를보고 있었지만, 일할 수는 없었다. –

답변

3

매우 구체적인 사용 사례가있는 경우가 아니면 사실상의 JSON 라이브러리로 Newtonsoft.Json을 사용하는 것이 좋습니다. 그것은 당신에게 많은 어려움을 줄여 줄 것입니다.

문제는 대신 배열이 사이트는 Dictionary에 단지 하나의 객체를

지도 객체를 반환한다는 것입니다. 여기에 답변

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! 그게 내가 찾고있는 것이고 네, NetonSoft JSON을 사용하고 있습니다. 그래서 사전 값 컬렉션에있는 모든 동전을 가지고 그냥 사전 값을 파고 : var blob = JsonConvert.DeserializeObject (json) .Values.ElementAt (0) .Values; –