locked
return object from json webservice RRS feed

  • Question

  • User589052352 posted

    Hi,

    I have webservice that should return json response.

    First I have a "Person" class with few properties.  Then I have datatable that has 5 rows. Each row is "person record" with name/dob etc columns which are basically properties of Person object. I m not sure if I need to have class of person but I created.

    Now how do I sent this datatable back to client? Do I need create an array of Person object ?

    Public Class Person
    
        Private _Name As String
        Private _dob As String
    
        Public Property Name() As String
            Get
                Return _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property
    
        Public Property DOB() As String
            Get
                Return _dob
            End Get
            Set(ByVal value As String)
                _dob = value
            End Set
        End Property
    
    End Class
    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.ComponentModel
    Imports System.Web.Script.Serialization
    Imports System.Web.Script.Services
    Imports System.Data.SqlClient
    
    ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    ' <System.Web.Script.Services.ScriptService()> _
    <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
    <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <ScriptService()> _
    Public Class Services
        Inherits System.Web.Services.WebService
     <WebMethod()> _
        <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
        Public Function getPerson() As  String
    
            Dim objperson As person= New person
    
            Dim sqlConnection As SqlConnection = _
            New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString)
    
            sqlConnection.Open()
    
            Dim sqlCommand As SqlCommand = New SqlCommand("GetPersons;", sqlConnection)
            Dim sqlDataset As DataSet = New DataSet()
            Dim sqlDataAdapter As SqlDataAdapter = New SqlDataAdapter(sqlCommand)
    
            sqlDataAdapter.Fill(sqlDataset)
            sqlConnection.Close()
    
            'I DONT KNOW WHAT TO DO NEXT....
             RETURN JSON/OBJECTS/??
    
    


    Thursday, February 28, 2013 11:25 PM

Answers

  • User-1758105784 posted

    For Using a List Type Person Class And Convert It to Json String ..So You Can Get All Value to client Side

    Eg:

    List<Person> person = //Here Fetch data from datatable

    Using a jsonConverter convert list to json type and send back to client side

    Hope it Help You

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 28, 2013 11:35 PM