Getting Values of a JSON Object using webmeothd in vb.net

China☆狼群 提交于 2019-12-12 01:28:48

问题


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


来源:https://stackoverflow.com/questions/33857007/getting-values-of-a-json-object-using-webmeothd-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!