simple json response from wcf

Hi, I want to get response from wcf to look like this:

{
  "12": {
    "id": "12",
    "car": "Camry Toyota",
    "year": "1998",
    "type": [
      [
        "red"
      ],
      [
        "black"
      ]
    ],
  }, and so on

but I am getting all types of response but not in this format. what I am doing wrong. here is the wcf code:

    <OperationContract()>
    <WebInvoke(UriTemplate:="car/{car_id}", Method:="GET", BodyStyle:=WebMessageBodyStyle.Wrapped, ResponseFormat:=WebMessageFormat.Json)>
    Function GetCar(ByVal car_id As String) As DataTable


and this is the funcion:

  Public Function GetCar(ByVal car_id As String) As DataTable Implements IRSAVService.GetCar
        Dim Table As New DataTable()
        Dim sQuery As String = ""
        sQuery = sQuery + " SELECT TOP 50 id, car, year"
        Using connection As New SqlConnection(ConfigurationManager.ConnectionStrings("carConnectionString").ConnectionString)
            Using command As New SqlCommand(sQuery, connection)
                command.CommandType = CommandType.Text
                connection.Open()
                Dim adapt As New SqlDataAdapter(command)
                Table.TableName = "car"
                adapt.Fill(Table)
            End Using
        End Using
        Return Table
    End Function


So. I want the response to be like the one I show above. And how I can construct the colors like that too?
LVL 1
rafaelrglAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MlandaTCommented:
Don't return a DataTable. Create a strongly type return value. Example:
Sub Main

	Dim objCar As New Car With {.id = 12, .car = "Toyota Corolla", .year = 2012}
	objCar.[type].Add(new string() {"red"}.toList())
	objCar.[type].Add(new string() {"purple"}.toList())

	Dim json As String = Newtonsoft.Json.JsonConvert.SerializeObject(objCar, Newtonsoft.Json.Formatting.Indented)
	
End Sub

' Define other methods and classes here
Public Class Car
	Public Property id As Integer
	Public Property car As String
	Public Property [year] As Integer
	
	Private _type As New List(Of List(Of String))
	Public ReadOnly Property [type] As List(Of List(Of String))
		Get
			Return _type
		End Get
	End Property
End Class

Open in new window

In your case, populate the class with values coming from your DataTable. The code snippet above gives the output shown below (this is from my LinqPad):
LinqPad snippetSo in your case, return a Car object as opposed to a DataTable:
    <OperationContract()>
    <WebInvoke(UriTemplate:="car/{car_id}", Method:="GET", BodyStyle:=WebMessageBodyStyle.Wrapped, ResponseFormat:=WebMessageFormat.Json)>
    Function GetCar(ByVal car_id As String) As Car

Open in new window

The Car object will be correctly serialised.
0
rafaelrglAuthor Commented:
Thanks. I will test it and let you know.
0
rafaelrglAuthor Commented:
if I want to have loop on the table and then add to the class, how can i do that

for each vrow in table.row
       how to add all rows inside the class, before return.
0
rafaelrglAuthor Commented:
other problem i ran into is when the vrow("year") is null, it does not pass the value to the class.
0
MlandaTCommented:
If you want to have a loop, then your function must return a List(Of Car) and not just Car.
Dim CarsList As New List(Of Car)
For Each dr As DataRow In dt.Rows

	Dim objCar As New Car()
	objCar.Car = CatchNull(dr("Car"), "")
	objCar.[Year] = CatchNull(dr("Year"), 0)
        '....the rest of the properties here...

	CarsList.Add(objCar)

Next

Open in new window

and CatchNull is a little function like
Public Shared Function Catchnull(ByVal objOld As Object, ByVal objNew As Object) As Object
   If Convert.IsDBNull(objOld) OrElse objOld Is Nothing Then
       Return objNew
   Else
       Return objOld
   End If
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WCF

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.