我有一个班级
public class Order
{
public int Id { get; set; }
public string ShippingMethod { get; set; }
}
我希望你能将下面的JSON数据反序列化为上述类/对象
string json = @"{
'Id': 1,
'ShippingMethod': {
'Code': 'external_DHLExpressWorldwide',
'Description': 'DHL ILS Express Worldwide'
}
}";
我的想法是JSON中的ShippingMethod
是一个对象,但我只想获取JSON中的ShippingMethod.Code
,并将其作为Order
类的string
在反序列化期间传递给ShippingMethod
。
如何使用Json.NET实现这个目标?
我相信我可以使用CustomJsonConverter来实现它。但是我感到困惑。文档中的示例仅涉及WriteJson
而不涉及ReadJson
。
Code
和Description
在ShippingMethod
中?它们如何序列化? - TinworShippingMethod
是 JSON 中的对象,也是 C# 类中的字符串。您可以如何映射它?您需要按照以下方式更改代码。 - Aruna