I am trying to convert JSON object to JSON Array, below is the JSON response and code I have done,
Link: http://api.fixer.io/latest?base=USD
JSON Object:
{ "base": "INR",
"date": "2017-01-05",
"rates": {
"AUD": 0.020187,
"BGN": 0.027416,
"BRL": 0.047298,
"CAD": 0.019577,
"CHF": 0.015004,
"CNY": 0.10136,
"CZK": 0.37877,
"DKK": 0.10421,
"GBP": 0.011977,
"HKD": 0.11415,
"HRK": 0.10621,
"HUF": 4.3213,
"IDR": 196.67,
"ILS": 0.056738,
"JPY": 1.7155,
"KRW": 17.533,
"MXN": 0.31322,
"MYR": 0.066019,
"NOK": 0.12646,
"NZD": 0.021148,
"PHP": 0.72882,
"PLN": 0.061229,
"RON": 0.06317,
"RUB": 0.87544,
"SEK": 0.13364,
"SGD": 0.021133,
"THB": 0.52705,
"TRY": 0.053295,
"USD": 0.01472,
"ZAR": 0.20079,
"EUR": 0.014018
}
}
And please find the below code
[HttpPost]
public async Task<ActionResult> Converter(Currency model)
{
string URL = "http://api.fixer.io/latest";
client.BaseAddress = new Uri(URL);
HttpResponseMessage response = new HttpResponseMessage();
response = client.GetAsync("?base=USD").Result;
string json = await response.Content.ReadAsStringAsync();
JObject jobj = JObject.Parse(json);
Response rps = JsonConvert.DeserializeObject<Response>(json);
return View();
}
And below are the classes used
public class Response
{
public string based { get; set; }
public string date { get; set; }
public List<Rate> rates { get; set; }
}
public class Rate
{
public string AUD {get; set;}
public string BGN {get; set;}
public string BRL {get; set;}
public string CAD {get; set;}
public string CHF {get; set;}
public string CNY {get; set;}
public string CZK {get; set;}
public string DKK {get; set;}
public string GBP {get; set;}
public string HKD {get; set;}
public string HRK {get; set;}
public string HUF {get; set;}
public string IDR {get; set;}
public string ILS {get; set;}
public string JPY {get; set;}
public string KRW {get; set;}
public string MXN {get; set;}
public string MYR {get; set;}
public string NOK {get; set;}
public string NZD {get; set;}
public string PHP {get; set;}
public string PLN {get; set;}
public string RON {get; set;}
public string RUB {get; set;}
public string SEK {get; set;}
public string SGD {get; set;}
public string THB {get; set;}
public string TRY {get; set;}
public string USD {get; set;}
public string ZAR {get; set;}
public string EUR {get; set;}
}
And below is the error I am getting
Server Error in '/' Application.
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type
'System.Collections.Generic.List`1[CurrencyConversion.Models.Rate]' because
the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or
change the deserialized type so that it is a normal .NET type (e.g. not a
primitive type like integer, not a collection type like an array or List<T>)
that can be deserialized from a JSON object. JsonObjectAttribute can also be
added to the type to force it to deserialize from a JSON object.
Path 'rates.AUD', line 1, position 49.