I am trying to access a .json endpoint with this line of code inside Main()
:
var RM= ParseArrayFromWeb<RM>("http://myendpoint.json").ToArray();
Here is the ParseArrayFromWeb
function that is defined above/outside of MAIN()
public static IEnumerable<T> ParseArrayFromWeb<T>(string url)
{
var webRequest = WebRequest.Create(url);
using (var response = webRequest.GetResponse())
{
if (response != null)
{
var stream = response.GetResponseStream();
if (stream != null)
{
using (var reader = new StreamReader(stream))
{
return JsonConvert.DeserializeObject<IEnumerable<T>>(reader.ReadToEnd());
}
}
}
throw new WebException("Options request returned null response");
}
}
Here is the RM class that is defined above/outside of Main() that should hold the json fields returned:
public class RM
{
public string calculation_method { get; set; }
public double? related_master_id { get; set; }
public string related_master_name { get; set; }
public string parameter_file_date { get; set; }
public string exchange_complex { get; set; }
public string combined_commodity_code { get; set; }
public string currency { get; set; }
public double? maintenance_margin { get; set; }
public double? scanning_risk { get; set; }
public double? spread_charge { get; set; }
public double? spot_charge { get; set; }
public double? inter_commodity_credit { get; set; }
public double? short_option_minimum { get; set; }
public double? scenario_number { get; set; }
public double? initial_margin { get; set; }
public string exchange_code { get; set; }
public string product_description { get; set; }
public string error_description { get; set; }
}
The error I am getting is:
Cannot deserialize JSON object into type 'System.Collections.Generic.IEnumerable`1[JSON.Program+RM]'.
HERE is one row of the JSON:
{"error_description":"error 454", "combined_commodity_code":"xx", "exchange_complex":"x", "exchange_code":"x", "initial_margin":null, "maintenance_margin":null, "scanning_risk":null, "spread_charge":null, "spot_charge":null, "currency":null," inter_commodity_credit":0},
Notice not all fields are present. That is ok. I have to handle that.
Any ideas?
Thank you.
> as the generic parameter for DeserializeObject. Or try using a non-generic overload first and check in the debugger what it deserializes to.