4 回答
TA贡献1859条经验 获得超6个赞
正如您的问题表明您正在使用json.net,您可以使用自定义Amount将您序列化为单个十进制值:JsonConverter
public class AmountConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Amount);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// Would decimal be more appropriate than double?
var value = serializer.Deserialize<double?>(reader);
if (value == null)
return null;
return new Amount(value.Value);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(((Amount)value).Value);
}
}
然后,您可以将其应用于您的模型,如下所示:
[JsonConverter(typeof(AmountConverter))]
public class Amount : IConvertible
{
private double _val = 0;
private int _decimal = 5;
public double Value { get { return _val; } }
public Amount()
{
}
public Amount(double amount)
: this()
{
// this.Value = amount;
_val = Math.Round(amount, _decimal);
}
#region IConvertible Members
#endregion
}
或者在JsonSerializerSettings.Converters序列化和反序列化的时候加入:
var settings = new JsonSerializerSettings
{
Converters = { new AmountConverter() },
};
var = JsonConvert.DeserializeObject<T>(json, settings);
TA贡献1876条经验 获得超5个赞
在浏览您的 JSON 时。
{ "FileVersion":"11.03", "ChargesDetail":{ "DiscountRate":0.0, "DiscountAmount":0.0 } }
public class ChargesDetail
{
public double DiscountRate { get; set; }
public Amount DiscountAmount { get; set; }
}
public class Amount:IConvertible
{
}
在这里,您需要将 DiscountAmount 的类型更改为 double,因为您映射的结构不正确。
或者,如果您将 JSON 更改为
{ "FileVersion":"11.03", "ChargesDetail":{ "DiscountRate":0.0, "DiscountAmount":{ "DiscountAmountVar":0.0 }} }
和类
public class ChargesDetail
{
public double DiscountRate { get; set; }
public Amount DiscountAmount { get; set; }
}
public class Amount:IConvertible
{
public double DiscountAmountVar{get;set;}
}
然后您之前的映射将起作用。
TA贡献1808条经验 获得超4个赞
在您的班级 Amount 中,您可以删除IConvertible并执行为:
public class Amount {
public decimal DiscountAmountVar { get; set; }
}
有了这个和你的其他课程:
public class ChargesDetail
{
public decimal DiscountRate { get; set; }
public Amount DiscountAmount { get; set; }
}
而这个 JSON:****EDIT****
string json =
@"{""DiscountRate"":12.0, ""DiscountAmount"":{ ""DiscountAmountVar"":13.0 } }";
**编辑:刚刚意识到您需要删除一级 Json。
您应该能够制作 JsonConvert。
主要代码:
class Program
{
static void Main(string[] args)
{
string json =
@"{""DiscountRate"":12.0, ""DiscountAmount"":{ ""DiscountAmountVar"":13.0 } }";
var converted = JsonConvert.DeserializeObject<ChargesDetail>(json);
Console.WriteLine(converted.DiscountAmount);
}
}
public class ChargesDetail
{
public decimal DiscountRate { get; set; }
public Amount DiscountAmount { get; set; }
}
public class Amount
{
public decimal DiscountAmountVar { get; set; }
}
- 4 回答
- 0 关注
- 187 浏览
添加回答
举报