2011-12-10 38 views
0

我想這樣的返回代碼(可以發現here):類定義返回嵌套的JSON結構,而不WCF

{"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 
} 

我當前的web服務的代碼如下:

******************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「,」LastN ame「:」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「 8姓氏「,」姓氏「:」8「,」姓氏「:」8姓氏「},{」姓氏「:」9姓氏「,」身份證號碼「:」9「,」姓氏「姓「:」 10firstname」,‘ID’:‘10’,‘名字’:‘10姓’}]

但正如你所看到的所需要的響應結構從層比我Employee類更復雜。我將如何能夠返回這樣的代碼?我應該在我的班級定義中做什麼?

哦,我不想使用WCF!

回答

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 

注意,錨和文字對象也。

我還建議將這些對象包裝到響應對象,除非您有不同的方法分別返回它們。

1

爲了做到這一點,你必須在代碼中創建一個嵌套的類結構。

例如,添加一個地址類:

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) 

然後在您的測試循環中的幾個地址添加到每個員工(我將把這部分留給你)。