ace*_*ura 6 c# rest json 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,"side":"buy","status":"ok"
,"symbol":"DIS","type":"limit"}}
Run Code Online (Sandbox Code Playgroud)
我从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; }
}
Run Code Online (Sandbox Code Playgroud)
用于反序列化的代码(来自Newtonsoft的JSON.NET):
Rootobject ord = JsonConvert.DeserializeObject<Rootobject>(responsebody);
Run Code Online (Sandbox Code Playgroud)
我收到以下错误.
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)
Run Code Online (Sandbox Code Playgroud)
我已经尝试将反序列化的结果保存到一个工作正常的"动态"对象.但我不想使用动态对象来映射字段.
请指教.
注意:第三方API也会发送一个名为"class"的字段.当我尝试直接调用字段时,如何在编译时出错时调用此方法.
你有fees产权的Order定义为类int,但在JSON文本是0.00000,即float或double。我认为您可能需要将该fees属性设置为float才能正确解析它。price和quantity属性的外观也相同。
| 归档时间: |
|
| 查看次数: |
8369 次 |
| 最近记录: |