0

I am trying to deserializing JSON file which looks like:

{"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],
...
}}

Here what i have done:

 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);
            }
        }

And it ends with an error :

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ApiTest.innerdata' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path 'dataset.data[0]', line 1, position 1122.

2 Answers 2

1

Try this:

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

In your json in data, you have an array which has an array inside it. The array inside has a datetime at index 0 and a number with decimal at index 1. In C# you cannot have an array with 2 types (date and number). You can use List<object> instead.

Sign up to request clarification or add additional context in comments.

Comments

1

It's to do with data "data":[["2016-12-01",1.0627] ,["2016-11-30",1.0635], As it stands this is an array of array's without a key pair. Should be more like the below

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

4 Comments

but i get this JSON file via API and i cant change it
Then your model is slightly wrong, you can copy the Json. Using visual studio you can then click edit > paste special > Json as class. This should create the model you need.
It will output data as public List<List<object>> data { get; set; } @codingyoshi beat me to it
@ChristianP upvoting your answer because that should have helped the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.