Getting Values of a JSON Object using webmeothd in

2019-09-08 17:39发布

问题:

I got stuck while getting value of a JSON object in vb.net. My JSON request posts data like given below:

function submitEmail() {
    var ClientsPersonalInfo = {
        FullName: $("#FullName").val(),
        PhoneNumber: $("#PhoneNumber").val(),
        EmailAddress: $("#EmailAddress").val(),
        DOB: $("#DOB").val(),
        Occupation: $("#Occupation").val(),
        NINumber: $("#NINumber").val(),
        FullAddress: $("#FullAddress").val()
    }

    var ClientsData = {};
    ClientsData.ClientsPersonalInfo = ClientsPersonalInfo;

    var d = '{"ClientsData":' + JSON.stringify(ClientsData) + '}'

    $.ajax({
        type: "POST",
        url: "add-new-client.aspx/SubmitEmail", // WebMethod Call
        data: d,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            alert(response)
        },
        failure: function (msg) {
            alert(msg);
        }
    });
}

JSON Object Looks Like

{
"ClientsPersonalInfo": {
    "FullName": "",
    "PhoneNumber": "",
    "EmailAddress": "",
    "DOB": "",
    "Occupation": "",
    "NINumber": "",
    "FullAddress": ""
    }
}

The above request returns an object in vb.net

VB Code:

<WebMethod()> _
    Public Shared Function SubmitEmail(ByVal ClientsPersonalInfo As Object) As String
        'What to do next to get object "ClientsPersonalInfo"
        'I want to access properties of the object like
        'Dim name As String = ClientsPersonalInfo.FullName

        Return "Successfully Converted."
    End Function

No I want to get values of this object and needs to append in a table. Please guide me how to get values of the above object? I am new in vb.net. Please guide. Thanks!

回答1:

First, you need to add the ClientsData and ClientsPersonalInfo classes to your web service:

Public Class ClientsPersonalInfo
     Public Property FullName As String
     Public Property PhoneNumber As String
     Public Property EmailAddress As String
     Public Property DOB As String
     Public Property Occupation As String
     Public Property NINumber As String
     Public Property FullAddress As String
 End Class

 Public Class RootObject
     Public Property ClientsPersonalInfo As ClientsPersonalInfo
 End Class

Now, you can simply change the parameter type in your web service method and .Net engine will do the parsing for you:

<WebMethod()> _
Public Shared Function SubmitEmail(ByVal MyClientsPersonalInfo As ClientsPersonalInfo) As String
    'You can access properties of the object like
    Dim name As String = MyClientsPersonalInfo.FullName

    Return "Successfully Converted."
End Function