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; }
    }
}
4

2 回答 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.

于 2019-03-05T20:39:28.180 回答
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
      }
    ]
  }
]
于 2019-03-05T20:58:07.163 回答