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.
In Deserialization, it does the opposite of Serialization which means it converts JSON string to custom . Net object. In the following code, it calls the static method DeserializeObject() of the JsonConvert class by passing JSON data. It returns a custom object (BlogSites) from JSON data.
A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array.
bind. JsonbException: Can't deserialize JSON array into: class exception occurs when you try to convert json string data to a java object. If the json string contains an array of objects and attempts to deserialize the json string to a json object, the array of objects could not be assigned to a single java object.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With