-2

I'm trying to deserialize json data but for some reason it always returns zero values. I have tried different ways but I'm not able to fetch that value

var result = JsonConvert.DeserializeObject<Tickers>(json);
       foreach (Rates rate in result)
         {
           Console.WriteLine(rate.EUR); 

         }

Json looks like this:

[
       {
          "base":"USD",
          "date":"2018-06-12",
          "rates":{
             "NZD":1.4053046000828844,
             "EUR":0.8288437629506838
             //plus many more
          }
       }
    ]

And I have these classes:

public class Tickers
{
    public string baseCurrency { get; set; }
    public string date { get; set; }
    public List<Rates> tickers { get; set; }
}

    public class Rates
{
    public double NZD { get; set; }
    public double EUR { get; set; }
    //etc

}

public partial class RootObject
{
    public string @base { get; set; }
    public string date { get; set; }
    public List<Rates> rates { get; set; }
}
  • 1
    This appears to be an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). The question in its current state is also incomplete and therefore unclear. Read [ask] and then edit the question to provide a [mcve] that can be used to reproduce the problem, allowing a better understanding of what is being asked. – Nkosi Mar 06 '19 at 11:23
  • 2
    I am confused. Why do you have a Tickers class and a RootObject class? Why do you attempt to deserialize an object of type Tickers class, despite the json data being a json array (that contains a json object not matching your Tickers class)? There is not much sense in what you have presented in your question, unfortunately... –  Mar 06 '19 at 11:24
  • I have tried with the RootObject but it doesn't work which is why I tried some more with another class. Deserializing into RO returns SerializationException – user10990200 Mar 06 '19 at 11:29
  • 1
    Yeah, and trying to deserialize into the Tickers type will also cause the deserializer to throw an execption (see my previous comment). "_I have tried with the RootObject_" Don't just try random, arbitrary stuff. Because that way more likely than not you will just get random, arbitrary program behavior instead of the expected/desired behavior. **Take a close and hard look at the structure of your json data**. It is a json array/list with json objects. Thus, you should deserialize an array/list/collection of a (class) type that **100% matches** the json objects inside the json array... –  Mar 06 '19 at 11:32
  • Don't [repost your question](https://stackoverflow.com/questions/55020402/deserializing-json-returns-0) if it gets closed as a duplicate. Edit it instead to explain why it isn't a duplicate. – CodeCaster Mar 06 '19 at 11:48
  • Well, this time I got several good responses instead of just the school hall way monitor showing off his power – user10990200 Mar 06 '19 at 13:42

2 Answers2

1

You are not deserializing the object properly

You can use below C# classes (Generated using quicktype)

public partial class Tickers
{
    [JsonProperty("base")]
    public string Base { get; set; }

    [JsonProperty("date")]
    public DateTimeOffset Date { get; set; }

    [JsonProperty("rates")]
    public Rates Rates { get; set; }
}

public partial class Rates
{
    [JsonProperty("NZD")]
    public double Nzd { get; set; }

    [JsonProperty("EUR")]
    public double Eur { get; set; }
}

And then you have an array of tickers to work with so deserialize like

var results = JsonConvert.DeserializeObject<List<Tickers>>(json);

then you can use it like

foreach (Tickers ticker in result)
{
    Console.WriteLine(ticker.Rates.Eur); 
}
Mihir Dave
  • 3,954
  • 1
  • 12
  • 28
0

You should iterate over result.tickers, as result is an object that contains the list of Rates.

var result = JsonConvert.DeserializeObject<Tickers>(json);
foreach (Rates rate in result.tickers)
{
    Console.WriteLine(rate.EUR); 
}
fhcimolin
  • 616
  • 1
  • 8
  • 27
  • Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Class.Tickers' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. – user10990200 Mar 06 '19 at 11:34
  • @user10990200 This is because your JSON is wrapped in square brackets. It's treating it like it's an array, not an object. You could either do `var result = JsonConvert.DeserializeObject>(json);` or replace the square brackets with empty spaces. – fhcimolin Mar 06 '19 at 11:39