I use Json.net JsonConvert to deserialize some JSON from the rest API in this way:
WebRequest request = WebRequest.Create(url); WebResponse ws = request.GetResponse(); using (StreamReader sr = new StreamReader(ws.GetResponseStream())) { jsonString = sr.ReadToEnd(); } RootObject root = JsonConvert.DeserializeObject<RootObject>(jsonString);
This works fine, waiting when trying to get a specific data set that, when eserialized, generates this error:
Error reading string. Unexpected token: StartObject. Path
I narrowed down the problem so that the API would return data in the wrong format for some records. Therefore, most of the returned data looks like this:
Income: { currency: { GBP: "48", USD: "32", EUR: "40" } },
However, in some cases, it returns:
income: { currency: { GBP: { GBP: "0", USD: "0", EUR: "0" }, USD: { GBP: "0", USD: "0", EUR: "0" }, EUR: { GBP: "0", USD: "0", EUR: "0" } }
In the short term, I can’t do anything to get the API fix, so I need to deal with this somehow, but I'm relatively new to using Json.net, so I'm not sure of the best way to do this. Any advice is appreciated.
The structure of the class I'm trying to deserialize is pretty simple:
public class RootObject { public List<Income> Income_list { get; set; } } public class Income { public Currency currency { get; set; } } public class Currency { public string GBP { get; set; } public string USD { get; set; } public string EUR { get; set; } }