Adam
Adam

Reputation: 6122

Class definition to return nested JSON structure without WCF

I'm trying to return code like this (can be found 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
} 

My current webservice code is as follows:

******************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

This returns:

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

But as you can see the required response structure from layer is more complex than my Employee class. How would I be able to return code like that? What should I do in my class definition?

Oh, and I DON'T want to use WCF!

Upvotes: 0

Views: 1097

Answers (2)

zero7
zero7

Reputation: 1298

Looking at one of your response objects you will need to return something like

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

Note that Anchor and Text are objects as well.

I would also recommend wrapping these objects to a response object unless you have different methods returning them separately.

Upvotes: 1

competent_tech
competent_tech

Reputation: 44931

In order to accomplish this, you have to create a nested class structure in your code.

For example, add an Address class:

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

and a list of addresses using this class to the Employee class:

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

Then in your test loop add a couple of addresses to each employee (I'll leave this part up to you).

Upvotes: 1

Related Questions