【发布时间】:2017-03-19 06:26:27
【问题描述】:
我有课
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 是一个对象,但我只想到达 ShippingMethod.Code(在 JSON 中),它将在反序列化期间作为 string 在 Order 类中传递给 ShippingMethod。
我怎样才能使用Json.NET 实现这个目标?
我相信我可以使用CustomJsonConverter 来完成它。但我很困惑。文档中的示例仅适用于 WriteJson,但不适用于 ReadJson。
【问题讨论】:
-
所以
Code和Description在ShippingMethod里面?如何序列化? -
你只需要“Id”和“Code”在同一级别?
-
是的,这是我的问题,也是我想要完成的目标
-
ShippingMethod是 json 中的对象和 C# 类中的字符串。你怎么能映射这个。您必须按如下方式更改代码。 -
@Aruna 查看我的映射方式stackoverflow.com/questions/40439290/…
标签: c# .net json serialization json.net