7

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.

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

5 Comments

How DUMB am I.. was trying to fix too many things :-(. Thanks a **TONN ** Jack. Thanks Again!!!
Also How can I call the field "class" in order to assign it to the internal field?
if you look at the JSON result, the API is returning a field called "class". Now when I try to read the same (code below), then I get a compile-error as "class" is a keyword. Console.WriteLine("Order Class : Class: {0}", ord.Order.class);
One solution immediately springs to mind which neatly avoids having to do some Json serializer foo is to change the name of the class in the json text to something that doesn't clash with a reserved word like root. I'm sure there is a better way, but saves getting stuck on it and you can always fix it later. You could use a reg expression for that.
Thank You again Jack. I shall use the _class that I renamed. Saved my day. Thank You!!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.