0

I am trying to deserialize json and clearly it's not going well since I'm here. I'm not only looking for help to make it work but also some link where it's explained how it works because I couldn't find one and I want to learn.

var json = w.DownloadString(url);

var data = JsonConvert.DeserializeObject<Rates[]>(json);

Json looks like this:

 {
   "info":"text",
   "sub":"text",
   "data":[
      {
         "date":20181111,
         "exchange":"New York",
         "open":1000.43,
         "high":1239.91,
         "low":1231.41
      },
      {
         "date":20181111,
         "exchange":"New York",
         "open":1000.43,
         "high":1239.91,
         "low":1231.41
      }
   ]
}

I have these classes:

    public class Rates
    {
        public List<Data> data { get; set; }
    }

    public class Data
    {
        public int date { get; set; }
        public string exchange { get; set; }
        public double open { get; set; }
        public double high { get; set; }
        public double low { get; set; }
    }

    public class RootObject
    {
        public string info { get; set; }
        public string sub { get; set; }
        public List<Data> data { get; set; }
    }
}
1
  • Looks like I messed it up while shortening it. I will try to fix it asap Commented Mar 5, 2019 at 20:39

2 Answers 2

1

There's two issues.

First is that your JSON is invalid (at least in the question itself).. you're missing an array closing bracket but I suspect it's a typo.

Other than that you want to deserialize into Rates class not an array of Rates.

var data = JsonConvert.DeserializeObject<Rates>(json);


To Enumerate through the Rates.. use the following:

foreach(Data rate in data.data){

    Console.WriteLine(rate.exchange);

}

Check out this dotnetfiddle.

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

5 Comments

Yes, it was a typo but it's fixed now. Thanks for that, but how do I loop through that list of rates? It's not enumerable
@user10990200 See the edit and updated dotnetfiddle.
@user10990200: Re: "how do I loop through that list of rates". You don't have a "list of rates", you have a rates instance that has two strings and a list of Data. The list of data is enumerable.
Thank you, accepted as correct. Is there any good reading to learn how to understand this? It's not the first time I'm having trouble with deserializing in c#
Well, depends what the json looks like.. If it's an array i.e. it looks like this: [{ x:1 }, { x:2 }] then you have an array but as you can see your opening brackets are { so its a single object.
0

Your json string returned represents the root object. Your classes look fine, you just deserialized into the wrong object. The Rates object seems unnecessary, unless you don't care about the 'info', and 'sub' fields. In that case you could deserialize into a Rates object. The extra properties would simply be ignored by the serializer.

You will want to deserialize the json into the root object.

var root = JsonConvert.DeserializeObject<RootObject>(json);

Then you can loop through the data objects like so:

foreach (Data dataItem in root.data)
{
    // Do something with data
}

Just as a sidenote, a Rates[] would have looked somewhat like this in json format:

[
  {
    "data": [
      {
         "date":20181111,
         "exchange":"New York",
         "open":1000.43,
         "high":1239.91,
         "low":1231.41
      },
      {
         "date":20181111,
         "exchange":"New York",
         "open":1000.43,
         "high":1239.91,
         "low":1231.41
      }
    ]
  },
  {
    "data": [
      {
         "date":20181111,
         "exchange":"New York",
         "open":1000.43,
         "high":1239.91,
         "low":1231.41
      },
      {
         "date":20181111,
         "exchange":"New York",
         "open":1000.43,
         "high":1239.91,
         "low":1231.41
      }
    ]
  }
]

Comments

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.