将JSON数据反序列化为数据集

时间:2017-08-21 10:51:17

标签: json dataset json-deserialization

我需要将JSON数据反序列化为数据集。

我的JSON数据如下:

{
  "USD" : {"15m" : 4066.93, "last" : 4066.93, "buy" : 4067.93, "sell" : 4065.92, "symbol" : "$"},
  "AUD" : {"15m" : 5134.32, "last" : 5134.32, "buy" : 5135.59, "sell" : 5133.05, "symbol" : "$"},
  "BRL" : {"15m" : 12798.27, "last" : 12798.27, "buy" : 12801.43, "sell" : 12795.1, "symbol" : "R$"},
  "CAD" : {"15m" : 5123.89, "last" : 5123.89, "buy" : 5125.15, "sell" : 5122.62, "symbol" : "$"},
  "CHF" : {"15m" : 3931.87, "last" : 3931.87, "buy" : 3932.84, "sell" : 3930.89, "symbol" : "CHF"},
  "CLP" : {"15m" : 2630893.79, "last" : 2630893.79, "buy" : 2631543.92, "sell" : 2630243.65, "symbol" : "$"},
  "CNY" : {"15m" : 27787.16, "last" : 27787.16, "buy" : 27813.84, "sell" : 27760.48, "symbol" : "¥"},
  "DKK" : {"15m" : 25746.42, "last" : 25746.42, "buy" : 25752.78, "sell" : 25740.05, "symbol" : "kr"},
  "EUR" : {"15m" : 3488.99, "last" : 3488.99, "buy" : 3501.16, "sell" : 3476.82, "symbol" : "€"},
  "GBP" : {"15m" : 3159.26, "last" : 3159.26, "buy" : 3160.04, "sell" : 3158.48, "symbol" : "£"},
  "HKD" : {"15m" : 31821.39, "last" : 31821.39, "buy" : 31829.25, "sell" : 31813.53, "symbol" : "$"},
  "INR" : {"15m" : 260529.25, "last" : 260529.25, "buy" : 260593.63, "sell" : 260464.87, "symbol" : "₹"},
  "ISK" : {"15m" : 430687.36, "last" : 430687.36, "buy" : 430793.79, "sell" : 430580.93, "symbol" : "kr"},
  "JPY" : {"15m" : 446446.04, "last" : 446446.04, "buy" : 446499.73, "sell" : 446392.34, "symbol" : "¥"},
  "KRW" : {"15m" : 4633691.67, "last" : 4633691.67, "buy" : 4634836.72, "sell" : 4632546.61, "symbol" : "₩"},
  "NZD" : {"15m" : 5562.31, "last" : 5562.31, "buy" : 5563.68, "sell" : 5560.93, "symbol" : "$"},
  "PLN" : {"15m" : 14804.08, "last" : 14804.08, "buy" : 14807.73, "sell" : 14800.42, "symbol" : "zł"},
  "RUB" : {"15m" : 240249.53, "last" : 240249.53, "buy" : 240308.9, "sell" : 240190.16, "symbol" : "RUB"},
  "SEK" : {"15m" : 32996.37, "last" : 32996.37, "buy" : 33004.52, "sell" : 32988.21, "symbol" : "kr"},
  "SGD" : {"15m" : 5540.54, "last" : 5540.54, "buy" : 5541.91, "sell" : 5539.17, "symbol" : "$"},
  "THB" : {"15m" : 134940.58, "last" : 134940.58, "buy" : 134973.92, "sell" : 134907.23, "symbol" : "฿"},
  "TWD" : {"15m" : 123374.24, "last" : 123374.24, "buy" : 123404.72, "sell" : 123343.75, "symbol" : "NT$"}
}

1 个答案:

答案 0 :(得分:0)

我找到了一种不使用任何外部API的方法来获取它

            using (var w = new WebClient())
            {
                var json_data = string.Empty;
                string url = "https://blockchain.info/ticker";
                // attempt to download JSON data as a string
                try
                {
                    json_data = w.DownloadString(url);
                    JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
                    var result = jsSerializer.DeserializeObject(json_data);
                    Dictionary<string, object> obj2 = new Dictionary<string, object>();
                    obj2=(Dictionary<string,object>)(result);
                    obj2 = (Dictionary<string, object>)obj2["USD"];
                    string Rate=obj2["last"].ToString();
                }
                catch (Exception) { }
                // if string with JSON data is not empty, deserialize it to class and return its instance 
            }