配置WCF数据协定以获得正确的JSON响应 [英] Configuring WCF data contract for proper JSON response
问题描述
我管理许多床位和床位早餐网站并运行一种预订引擎,以帮助客户直接在其网站上进行预订(而不是去另一个网站完成交易).
I manage a number of Bed & Breakfast websites and run a kind of booking engine to help the client do bookings directly on their website (as opposed to going to another site to close the deal).
我正在使用ASP.NET WCF创建一个Web服务,以创建一个关于我所管理酒店的客房可用性的JSON响应.我已经配置了Web服务以序列化JSON中的响应.一切工作都很好,除了我无法完全构造合适的对象来获取所需的JSON响应.
I am working on creating a web service using ASP.NET WCF to create a JSON response about room availability of the hotels I manage. I have configured the web service to serialize the response in JSON. Everything works reasonable well EXCEPT I cannot quite construct the proper objects to get the JSON response I want.
为了正确进行切换,我需要返回以以下方式构造的JSON响应:
In order do the handoff properly, I am required to return a JSON response structured in the following way:
{
"api_version" : 10 ,
"lang" : "en_US",
"hotels" :
[
{
"name" : "Example Hotel",
"email" : "concierge@example.com",
"phone" : "555-555-5555",
"fax" : "555-555-5555",
"room_types" :
{
"Orchid Room" :
{
"url" : "http://hotel.com/orchid",
"desc" : "One queen bed etc."
},
"Presidential Suite" :
{
"url" : "http://hotel.com/predidential",
"desc" : "One king bed etc."
}
}
}
]
}
我将我的WCF服务配置为以JSON序列化返回对象没有问题.但是,令我感到困惑的是如何构造对象的"room_types"部分.现在的问题是,对于我系统中的每个酒店,我将拥有不同的房间类型,因此我需要以某种方式动态生成room_types对象.但是我不知道如何正确地构造对象.
I have no problem configuring my WCF service to return objects in JSON serialization. However, where I am confused is how to construct the "room_types" part of the object. Now, the issue is that for each hotel in my system I will have DIFFERENT room types, so I need to somehow dynamically generate the room_types object. But I don't know to structure the objects properly.
以下是我到目前为止设置的数据合约,这些合约没有正确序列化为JSON:
Here are the data contracts I have set up so far which do not serialize into JSON properly:
<DataContract()>
Public Class InventoryResponseObject
<DataMember(order:=0)> Public Property api_version As Integer
<DataMember(order:=1)> Public Property lang As String
<DataMember(order:=2)> Public Property hotels As hotelsObject
<DataMember(order:=3)> Public Property errors As List(Of String)
End Class
<CollectionDataContract()>
Public Class hotelsObject
Inherits List(Of HotelObject)
Public Sub New()
End Sub
End Class
<DataContract()>
Public Class HotelObject
<DataMember(order:=0)> Public Property name As String
<DataMember(order:=1)> Public Property email As String
<DataMember(order:=2)> Public Property phone As String
<DataMember(order:=3)> Public Property fax As String
<DataMember(order:=4)> Public Property room_types As RoomTypeCollectionObject
End Class
<DataContract()>
Public Class RoomTypeInfoObject
<DataMember(order:=0)> Public Property url As String
<DataMember(order:=1)> Public Property desc As String
End Class
<CollectionDataContract()>
Public Class RoomTypeCollectionObject
Inherits List(Of RoomTypeInfoObject)
Public Sub New()
End Sub
End Class
这将产生以下不正确的JSON重新操作:
This will produce the following incorrect JSON resopnse:
{
"api_version" : 10 ,
"lang" : "en_US",
"hotels" :
[
{
"name" : "Example Hotel",
"email" : "concierge@example.com",
"phone" : "555-555-5555",
"fax" : "555-555-5555",
"room_types" :[
{
"url" : "http://hotel.com/orchid",
"desc" : "One queen bed etc."
},
{
"url" : "http://hotel.com/predidential",
"desc" : "One king bed etc."
}
]
}
]
}
我很困惑.任何帮助将不胜感激.
I am stumped. Any assistance would be hugely appreciated.
推荐答案
好,我找到了解决方案.它需要两个步骤:
Ok I found a solution. It required two steps:
- 使用ExpandoObject将属性动态添加到room_types对象
- 使用Newtonsoft.Json序列化返回对象,并强制服务返回纯文本
这篇关于配置WCF数据协定以获得正确的JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!