用于返回没有WCF的嵌套JSON结构的类定义

时间:2011-12-10 22:20:12

标签: asp.net vb.net web-services class

我正在尝试返回这样的代码(可以找到here):

{"hotspots": [{
  "id": "test_1",
    "anchor": { "geolocation": { "lat": 52.3729, "lon": 4.93 } },  
    "text": {
    "title": "The Layar Office", 
    "description": "The Location of the Layar Office", 
    "footnote": "Powered by Layar" },
  "imageURL": "http:\/\/custom.layar.nl\/layarimage.jpeg",
 }], 
 "layer": "snowy4",
 "errorString": "ok", 
 "errorCode": 0
} 

我目前的网络服务代码如下:

******************RestServiceImpl.vb

Imports System.ServiceModel
Imports System.ServiceModel.Web
Imports System.IO
Imports System.ServiceModel.Activation
Imports System.Web.Script.Serialization
Imports System.Collections.Generic

Namespace RestService

Public Class Employee
    Public Property Id() As String
        Get
            Return m_Id
        End Get
        Set(value As String)
            m_Id = Value
        End Set
    End Property
    Private m_Id As String
    Public Property FirstName() As String
        Get
            Return m_FirstName
        End Get
        Set(value As String)
            m_FirstName = Value
        End Set
    End Property
    Private m_FirstName As String
    Public Property LastName() As String
        Get
            Return m_LastName
        End Get
        Set(value As String)
            m_LastName = Value
        End Set
    End Property
    Private m_LastName As String
End Class

<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
Public Class RestServiceImpl
    Implements IRestServiceImpl

    Public Function JSONData(ByVal lat As String, ByVal lng As String, ByVal d As String, ByVal cat As String) As List(Of Employee) Implements IRestServiceImpl.JSONData
        Dim json As New JavaScriptSerializer()
        Dim l As New List(Of Employee)

        Dim e As Employee
        For i As Integer = 0 To 10
            e = New Employee
            e.Id = i.ToString
            e.FirstName = i.ToString + "firstname"
            e.LastName = i.ToString + " lastname"
            l.Add(e)
        Next i
        Return l
    End Function

End Class
End Namespace



******************IRestServiceImpl.vb

Imports System.ServiceModel
Imports System.ServiceModel.Web
Imports System.IO
Imports System.ServiceModel.Activation
Imports System.Collections.Generic

Namespace RestService
<ServiceContract()> _
Public Interface IRestServiceImpl
    <OperationContract()> _
    <WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Bare, UriTemplate:="api/objects/json/?lat={lat}&lon={lon}&radius={radius}&cat={cat}")> _
    Function JSONData(ByVal lat As String, ByVal lon As String, ByVal radius As String, ByVal cat As String) As List(Of Employee)
End Interface

End Namespace

返回:

[{“FirstName”:“0firstname”,“Id”:“0”,“LastName”:“0 lastname”},{“FirstName”:“1firstname”,“Id”:“1”,“LastName” “:”1 lastname“},{”FirstName“:”2firstname“,”Id“:”2“,”LastName“:”2 lastname“},{”FirstName“:”3firstname“,”Id“:”3 “,”LastName“:”3 lastname“},{”FirstName“:”4firstname“,”Id“:”4“,”LastName“:”4 lastname“},{”FirstName“:”5firstname“,”Id “:”5“,”LastName“:”5 lastname“},{”FirstName“:”6firstname“,”Id“:”6“,”LastName“:”6 lastname“},{”FirstName“:”7firstname “,”Id“:”7“,”LastName“:”7 lastname“},{”FirstName“:”8firstname“,”Id“:”8“,”LastName“:”8 lastname“},{”FirstName “:”9firstname“,”Id“:”9“,”LastName“:”9 lastname“},{”FirstName“:”10firstname“,”Id“:”10“,”LastName“:”10 lastname“} ]

但正如您所看到的,层中所需的响应结构比我的Employee类更复杂。我怎么能这样返回代码?我应该怎么做我的班级定义?

哦,我不想使用WCF!

2 个答案:

答案 0 :(得分:1)

为了实现这一点,您必须在代码中创建一个嵌套的类结构。

例如,添加一个Address类:

Public Class Address
    Public Property Type As String
    Public Property Address1 As String
    Public Property Address2 As String
    Public Property City As String
    Public Property State As String
End Class

以及使用此类到Employee类的地址列表:

Public Property Addresses As New System.Collections.Generic.List(Of Address)

然后在你的测试循环中为每个员工添加几个地址(我将把这部分留给你)。

答案 1 :(得分:1)

查看您的某个响应对象,您需要返回类似

的内容
Public Class Hotspots
        public int id { get; set; }
        public Anchor anchor { get; set; }
        public Text text { get; set; }
        public string imageURL { get; set; }
        ....
End Class

请注意,Anchor和Text也是对象。

我还建议将这些对象包装到响应对象,除非您有不同的方法单独返回它们。