我有一个json,我正在使用jsonconvert输出float。原来的json有整数,我试图转换成美元和美分(转换为375到3.75)。
JSON:
[{"returnValue":
[{"facility":"DRIO","recrd_desc":"DEFAULT","update_time":{"$date":1509128545000},"control_num":1,"starttime":{"$date":1506830400000},"endtime":{"$date":4102462799000},"can_exchange_rate":2000},
{"facility":"DRIO","recrd_desc":"TEMP","update_time":{"$date":1521229607000},"control_num":37,"starttime":{"$date":1513040240000},"endtime":{"$date":1544576240000},"can_exchange_rate":2112},
{"facility":"DRIO","recrd_desc":"TEMP 3","update_time":{"$date":1521229399000},"control_num":38,"starttime":{"$date":1544576580000},"endtime":{"$date":1576112580000},"can_exchange_rate":2000}],
"ok":1.0}]
型号:
namespace CSCApi.Models
{
public class FareChartTypeResponse
{
public List<FareChartType> ReturnValue { get; set; }
public decimal ok { get; set; }
}
public class FareChartType
{
public string facility { get; set; }
public string recrd_desc { get; set; }
public Update_Time update_time { get; set; }
public int control_num { get; set; }
public Starttime starttime { get; set; }
public Endtime endtime { get; set; }
[JsonConverter(typeof(CentsToDollarsConverter))]
public float can_exchange_rate { get; set; }
}
public class CentsToDollarsConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(System.Int32).Equals(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var input = (int)reader.Value;
float retValue = (input / 100.00F);
return retValue;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(((float)value)*100);
}
}
}
我一直收到错误 无法将当前JSON数组(例如[1,2,3])反序列化为类型。
我无法弄清楚错误的位置。任何帮助表示赞赏。 谢谢 NH