3

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.  

2 Answers 2

4

rates is not an array, just an object. So change

public List<Rate> rates { get; set; }

to

public Dictionary<string,double> rates { get; set; }

That way, you don't also need this big Rate class.

Your final class

public class Response
{
    public string based { get; set; }
    public string date { get; set; }
    public Dictionary<string,double> rates { get; set; }
}

BTW: You don't need JObect.Parse in your code. You can give the json string directly to JsonConvert.DeserializeObject

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

3 Comments

Are you sure that the json will bind to Dictionary without key and value property?
@Developer I am sure also
@Developer A JSO essentially is a dictionary, described on JSON.org as "A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array."
2

You don't have a List of Rate you have a Rate object (in your json)

Try this

public class Response
{
    public string @base { get; set; }
    public string date { get; set; }
    public Rate rates { get; set; }
}

Also, please note that your based property in Response won't currently map to your JSON. In your JSON, it's base
base is a reserved keyword, hence the @base property in my version of the Response class

2 Comments

This answer also addresses the base mis-map, which the currently upvoted answer does not.
Right, but it is more resilient way for added/removed currencies in the future. So my vote is for the other answer.

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.