Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error on Convert JSON object to JSON Array

Tags:

json

c#

json.net

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.  
like image 621
best Avatar asked Sep 20 '26 22:09

best


2 Answers

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

like image 56
L.B Avatar answered Sep 23 '26 12:09

L.B


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

like image 20
Alex Avatar answered Sep 23 '26 11:09

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!