使用JSON.Net Serializer序列化对象时更改JsonProperty名称

时间:2013-03-02 01:38:30

标签: .net vb.net json json.net

我有:

Public Class Ticket
  Private _ITicketStyle as ITicketStyle
  Public Property TicketStyle() as ITicketStyle
    Get
      Return _ITicketStyle
    End Get
    Set(value as ITicketStyle)
      _ITicketStyle = value
    End Set
  End Property
End Class

Public Class FlightTicket
    Implements ITicketStyle
    Public Property city() as String
End Class

Public Class MovieTicket
    Implements ITicketStyle
    Public Property name() as String
End Class

使用:

Dim ft as New Ticket()
ft.Ticket = New FlightTicket With { .city="NY" }
Dim output1 as String = JsonConvert.SerializeObject(ft) 

我得到:output1 = {"TicketStyle":{"city" : "NY"}}

和:

Dim mt as New Ticket()
mt.Ticket = New MovieTicket With { .name="Transformers" }
Dim output2 as String = JsonConvert.SerializeObject(mt)

我得到:output2 = {"TicketStyle":{"name" : "Transformers"}}

但我需要:

output1 = {"FlightTicket":{"city": "NY"}} and 
output2={"MovieTicket": {"name": "Transformers"}}

有没有办法实现这个?我可以将JsonProperty("FlightTicket")应用于TicketStyle属性,但需要在将MovieTicket对象分配给TicketStyle属性时将其更改为“MovieTicket”。

0 个答案:

没有答案