在vb.net中使用webmeothd获取JSON对象的值

时间:2015-11-22 16:14:18

标签: json ajax vb.net webmethod

我在vb.net中获取JSON对象的价值时遇到困难。我的JSON请求发布如下所示的数据:

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对象看起来像

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

上述请求返回vb.net中的对象

VB代码:

<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

不,我想获取此对象的值,需要附加到表中。请指导我如何获取上述对象的值?我是vb.net的新手。请指导。谢谢!

1 个答案:

答案 0 :(得分:1)

首先,您需要将ClientsDataClientsPersonalInfo类添加到您的网络服务中:

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

现在,您只需更改Web服务方法中的参数类型,.Net引擎就可以为您解析:

<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