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": [
  }, 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:

    <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
                Dim adapt As New SqlDataAdapter(command)
                Table.TableName = "car"
            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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

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))
			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:
    <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.
rafaelrglAuthor Commented:
Thanks. I will test it and let you know.
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.
rafaelrglAuthor Commented:
other problem i ran into is when the vrow("year") is null, it does not pass the value to the class.
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...



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
       Return objOld
   End If
End Function

Open in new window

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

From novice to tech pro — start learning today.