无法反序列化JSON结果在C#。输入的字符串不是正确的格式错误 [英] Unable to deserialize the JSON result in C#. Input string is not in a correct format error

查看:3071
本文介绍了无法反序列化JSON结果在C#。输入的字符串不是正确的格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图反序列化JSON输出到C#对象。
JSON结果:

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"}}

这是JSON我的派生类:

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; }
}

代码用于(从Newtonsoft JSON.NET)反序列化:

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.

请咨询

请注意:另外,第三方API发送一个名为类字段。我如何把这个作为我得到编译时错误,当我尝试直接调用现场。

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.

推荐答案

您有费用定义为 INT 而在订单类属性JSON的文本是 0.00000 ,即浮动双击。我想你可能需要让费用属性到浮动,以便正确地解析它。查找价格属性了。

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.

这篇关于无法反序列化JSON结果在C#。输入的字符串不是正确的格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆