当JSON变量名无效且根数组不在Key,Value对中时,如何在C#中反序列化JSON?

时间:2014-06-18 17:52:23

标签: c# json restsharp

通常,人们会创建一个类甚至是嵌套类来反序列化大多数JSON 然后将JSON提供给Json.Net或其他一些库。

这就是我做的,直到我遇到这个JSON。

{
  "USD" : {"15m" : 609.09, "last" : 609.09, "buy" : 606, "sell" : 609.09,  "symbol" : "$"},
  "CNY" : {"15m" : 3783.32, "last" : 3783.32, "buy" : 3764.13, "sell" : 3783.32,  "symbol" : "¥"},
  "JPY" : {"15m" : 62194.06, "last" : 62194.06, "buy" : 61878.54, "sell" : 62194.06,  "symbol" : "¥"},
  "SGD" : {"15m" : 761.65, "last" : 761.65, "buy" : 757.79, "sell" : 761.65,  "symbol" : "$"},
  "HKD" : {"15m" : 4721.51, "last" : 4721.51, "buy" : 4697.55, "sell" : 4721.51,  "symbol" : "$"},
  "CAD" : {"15m" : 663.64, "last" : 663.64, "buy" : 660.28, "sell" : 663.64,  "symbol" : "$"},
  "NZD" : {"15m" : 711.55, "last" : 711.55, "buy" : 707.94, "sell" : 711.55,  "symbol" : "$"},
  "AUD" : {"15m" : 648.34, "last" : 648.34, "buy" : 645.05, "sell" : 648.34,  "symbol" : "$"},
  "CLP" : {"15m" : 337004.93, "last" : 337004.93, "buy" : 335295.25, "sell" : 337004.93,  "symbol" : "$"},
  "GBP" : {"15m" : 362.9, "last" : 362.9, "buy" : 361.06, "sell" : 362.9,  "symbol" : "£"},
  "DKK" : {"15m" : 3356.52, "last" : 3356.52, "buy" : 3339.49, "sell" : 3356.52,  "symbol" : "kr"},
  "SEK" : {"15m" : 4068.99, "last" : 4068.99, "buy" : 4048.35, "sell" : 4068.99,  "symbol" : "kr"},
  "ISK" : {"15m" : 69486.21, "last" : 69486.21, "buy" : 69133.69, "sell" : 69486.21,  "symbol" : "kr"},
  "CHF" : {"15m" : 548.15, "last" : 548.15, "buy" : 545.37, "sell" : 548.15,  "symbol" : "CHF"},
  "BRL" : {"15m" : 1354.87, "last" : 1354.87, "buy" : 1348, "sell" : 1354.87,  "symbol" : "R$"},
  "EUR" : {"15m" : 450.11, "last" : 450.11, "buy" : 447.83, "sell" : 450.11,  "symbol" : "€"},
  "RUB" : {"15m" : 20948.52, "last" : 20948.52, "buy" : 20842.25, "sell" : 20948.52,  "symbol" : "RUB"},
  "PLN" : {"15m" : 1851.37, "last" : 1851.37, "buy" : 1841.98, "sell" : 1851.37,  "symbol" : "zł"},
  "THB" : {"15m" : 19801.3, "last" : 19801.3, "buy" : 19700.84, "sell" : 19801.3,  "symbol" : "฿"},
  "KRW" : {"15m" : 619112.58, "last" : 619112.58, "buy" : 615971.73, "sell" : 619112.58,  "symbol" : "₩"},
  "TWD" : {"15m" : 18274.36, "last" : 18274.36, "buy" : 18181.65, "sell" : 18274.36,  "symbol" : "NT$"}      
}

1 个答案:

答案 0 :(得分:0)

第一个问题是根数组不在名称值对中 第二个是“15m”密钥不是有效的C#变量名。 (我正在计划用字符串代替其他东西,但我觉得它太难看了)

这是一个超级简单的解决方案:

using System;
using System.Collections.Generic;
using System.Linq;
using RestSharp;
using RestSharp.Deserializers;

namespace JSONRestTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Requesting data...");

            //create new RestSharp Client and tell it the Host
            RestClient client = new RestClient("http://blockchain.info");

            //Tell it the path and any variables if needed
            RestRequest request = new RestRequest("/ticker", Method.GET);
            //request.AddParameter("myParameter", myVariable);

            //Tell RestSharp to expect JSON
            request.RequestFormat = DataFormat.Json;

            //Execute the request
            IRestResponse response = client.Execute(request);

            //Create a JSON Deserializer
            JsonDeserializer deserial = new JsonDeserializer();

            //Deserialize to a Dictionary Object 
            Dictionary<string, Rate> jsonObj = deserial.Deserialize<Dictionary<string, Rate>>(response);

            //Loop through the Dictionary
            foreach (KeyValuePair<string, Rate> currency in jsonObj)
            {
                Console.WriteLine(currency.Key + " Rate: " + currency.Value.last.ToString());
            }

            Console.WriteLine("Done...");            
        }
    }

    public class Rate
    {        
        public float _15m { get; set; }
        public double last { get; set; }
        public double buy { get; set; }
        public double sell { get; set; }
        public string symbol { get; set; }
    }      
}

诀窍是对所有非键控数据使用Dictionary对象。这解决了第一个问题 接下来是使用RestSharp并在违规变量的前面放置一个“_”。