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

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

我在获取 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": ""
    }
}

上面请求returns一个对象在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

不,我想获取此对象的值,需要附加一个 table。请指导我如何获取上述对象的值?我是 vb.net 的新人。请指导。谢谢!

首先,您需要将 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