Unable to deserialize the JSON result in C#. Input string is not in a correct format error
You have the fees
property in the Order
class defined as an int
but in the JSon text it is 0.00000
, i.e. a float
or double
. I think you may need to make the fees
property into a float
in order to parse it properly. Looks the same for the price
and quantity
properties too.
Related videos on Youtube
aceventura
Updated on June 21, 2022Comments
-
aceventura almost 2 years
I am trying to deserialize a json output to a C# object. JSON result:
{"order":{"commission":3.490000,"cost":4.490000,"duration":"day","extended_hours ":false,"fees":0.000000,"class":"equity","price":1.000000,"quantity":1.000000,"r equest_date":"2013-11-26T09:43:17.118Z","result":true,"side":"buy","status":"ok" ,"symbol":"DIS","type":"limit"}}
My derived class from JSON:
public class Rootobject { public Order Order { get; set; } } public class Order { public float commission { get; set; } public float cost { get; set; } public string duration { get; set; } public bool extended_hours { get; set; } public int fees { get; set; } public string _class { get; set; } public int price { get; set; } public int quantity { get; set; } public DateTime request_date { get; set; } public bool result { get; set; } public string side { get; set; } public string status { get; set; } public string symbol { get; set; } public string type { get; set; } }
Code used to deserialize (JSON.NET from Newtonsoft) :
Rootobject ord = JsonConvert.DeserializeObject<Rootobject>(responsebody);
I am getting the following error.
Unhandled Exception: System.FormatException: Input string was not in a correct format. at Newtonsoft.Json.Utilities.ConvertUtils.Int32Parse(Char[] chars, Int32 start, Int32 length) at Newtonsoft.Json.JsonTextReader.ParseNumber() at Newtonsoft.Json.JsonTextReader.ParseValue() at Newtonsoft.Json.JsonTextReader.ReadInternal() at Newtonsoft.Json.JsonReader.ReadAsInt32Internal() at Newtonsoft.Json.JsonTextReader.ReadAsInt32() at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(Jso nReader reader, JsonContract contract, Boolean hasConverter)
I have tried saving the deserialized result to a "dynamic" object which works fine. But I do not want to use the dynamic object for mapping the fields.
Please advice.
Note: Also the 3rd party API is sending a field called "class". How do I call this as I get compile-time error when I try to directly call the field.