JSON反序列化

时间:2016-12-02 21:35:55

标签: c# json serialization

我正在尝试反序列化JSON文件,如下所示:

{"dataset":{"id":14686248,
"dataset_code":"EURUSD",
"database_code":"ECB",
"name":"EUR vs USD Foreign Exchange Reference Rate",
"description":"Euro (EUR) vs. US Dollar (USD) reference exchange rate. Foreign exchange reference rates are published by the European Central Bank.  Reference rates are usually updated by 3:00pm CET, based on a regular daily concertation procedure between various central banks across Europe and around the world.  This procedure normally takes place at 2:15pm CET.  Reference rates are mid-market rates, calculated as averages of the buying and selling rate; they do not necessarily reflect actual transaction rates.  Euro foreign exchange reference rates are always quoted using the 'certain' method (i.e EUR 1 = X foreign currency units, where X is the published reference rate).",
"refreshed_at":"2016-12-01T23:16:13.829Z",
"newest_available_date":"2016-12-01",
"oldest_available_date":"1999-01-04",
"column_names":["Date","Value"],
"frequency":"daily",
"type":"TimeSeries",
"premium":false,
"limit":null,
"transform":null,
"column_index":null,
"start_date":"1999-01-04",
"end_date":"2016-12-01",
"data":[["2016-12-01",1.0627]
,["2016-11-30",1.0635],
...
}}

我在这里做了什么:

 class request
    {
        [JsonProperty("dataset")]
        public dataset dataset { get; set; }
    }

 class dataset
    {
        public int id { get; set; }
        public string dataset_code { get; set; }
        public string database_code { get; set; }
        public string name { get; set; }
        public string description { get; set; }
        public string refreshed_at { get; set; }
        public DateTime newest_available_date { get; set; }
        public string[] column_names { get; set; }
        public string frequency { get; set; }
        public string type { get; set; }
        public DateTime oldest_available_date { get; set; }
        public bool premium { get; set; }
        public string column_index { get; set; }
        public DateTime start_date { get; set; }
        public DateTime end_date { get; set; }
        [JsonProperty("data")]
        public List<innerdata> data { get; set; }
    }
 class Data
    {
        public List<innerdata> data { get; set; }
    }
 class innerdata
    {
        public DateTime date { get; set; }
        public double rate { get; set; }



static void Main(string[] args)
        {
            try
            {
                using (var client = new HttpClient())
                {
                    string result = client.GetStringAsync(MakeQuery()).Result;
                    var weatherData = JsonConvert.DeserializeObject<request> (result);
                }



            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occured: " + ex.Message);
            }
        }

它以错误结束:

  

无法将当前JSON数组(例如[1,2,3])反序列化为类型   'ApiTest.innerdata'因为类型需要JSON对象(例如   {“name”:“value”})正确反序列化。要修复此错误   将JSON更改为JSON对象(例如{“name”:“value”})或更改   反序列化类型为数组或实现集合的类型   接口(例如ICollection,IList)就像List一样   从JSON数组反序列化。也可以添加JsonArrayAttribute   到类型强制它从JSON数组反序列化。路径   'dataset.data [0]',第1行,第1122位。

2 个答案:

答案 0 :(得分:1)

与数据有关 "data":[["2016-12-01",1.0627] ,["2016-11-30",1.0635], 目前,这是一个没有密钥对的阵列数组。应该更像下面的

data":[{"date": "2016-12-01","rate":1.0627}
,{"date":"2016-11-30","rate":1.0635}]

答案 1 :(得分:1)

试试这个:

[JsonProperty( "data" )]
public List<List<object>> data { get; set; }

在你的json数据中,你有一个数组里面有一个数组。里面的数组在索引0处有一个日期时间,在索引1处有一个带小数的数字。在C#中,你不能有一个包含2种类型(日期和数字)的数组。您可以改为使用List<object>