无法在C#中反序列化JSON结果。输入字符串格式不正确错误
我正在尝试将json输出反序列化到C#对象。 JSON结果:无法在C#中反序列化JSON结果。输入字符串格式不正确错误,c#,json,rest,json.net,json-deserialization,C#,Json,Rest,Json.net,Json Deserialization,我正在尝试将json输出反序列化到C#对象。 JSON结果: {"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,"si
{"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派生类:
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):
我已经尝试将反序列化的结果保存到一个“动态”对象中,该对象运行良好。但是我不想使用动态对象来映射字段
请给我一些建议
注意:第三方API也发送一个名为“类”的字段。当我试图直接调用该字段时,由于出现编译时错误,如何调用该字段。您在
Order
类中有fees
属性,定义为int
,但在JSon文本中它是0.00000
,即float
或double
。我认为您可能需要将fees
属性设置为float
,以便正确解析它。在价格
和数量
属性上看起来也一样。我有多笨。。试图修复的内容太多了:-(.谢谢**吨**杰克。再次感谢!!!我如何调用字段“class”以将其分配给内部字段?如果查看JSON结果,API将返回一个名为“class”的字段。现在,当我尝试读取相同的字段(下面的代码)时,我会得到一个编译错误“class”是关键字.Console.WriteLine(“订单类:类:{0}”,Order.Order.Class);我马上想到了一个解决方案,它巧妙地避免了使用Json序列化程序foo,即将Json文本中的类的名称更改为与保留字(如root
)不冲突的名称。我相信有更好的方法,但可以避免被卡住,您可以随时稍后修复。您可以告诉我们这是一个reg表达式。再次感谢你,杰克。我将使用我重新命名的_类。拯救了我的一天。谢谢!!!
Rootobject ord = JsonConvert.DeserializeObject<Rootobject>(responsebody);
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)