我有:
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”。