从Ajax Post访问对象中的项目并将其用作数据表

时间:2015-10-12 07:22:11

标签: jquery json ajax vb.net

我有这个JSON字符串数据

{"data":[{"Action":"<button class=\"btn btn-primary btn-xs\" data-toggle=\"tooltip\" title=\"Edit Item\" id=\"editLineItm\" ><i class=\"fa fa-edit fa-fw\"></i></button> <button class=\"btn btn-danger btn-xs\" data-toggle=\"tooltip\" title=\"Remove Item\" id=\"deleteLineItm\"><i class=\"fa fa-times fa-fw\"></i></button>","LineId":0,"ServiceDesc":"Paragon","LineDescription":"3123","LineQty":"123.00","LineUnitPrice":"123.00","ContractId":"3","ServiceId":"2","LineSum":"15,129.00"},{"Action":"<button class=\"btn btn-primary btn-xs\" data-toggle=\"tooltip\" title=\"Edit Item\" id=\"editLineItm\" ><i class=\"fa fa-edit fa-fw\"></i></button> <button class=\"btn btn-danger btn-xs\" data-toggle=\"tooltip\" title=\"Remove Item\" id=\"deleteLineItm\"><i class=\"fa fa-times fa-fw\"></i></button>","LineId":1,"ServiceDesc":"IPVPN","LineDescription":"3","LineQty":"123.00","LineUnitPrice":"1.00","ContractId":"1","ServiceId":"4","LineSum":"123.00"}]}

来自AJAX帖子 我希望将json对象中的项目访问到我的webservice中的web方法:这是我的代码

<WebMethod()> _
Public Function invExecInvLines(ByVal data As String)
    Dim t As DataTable = DirectCast(JsonConvert.DeserializeObject(data, (GetType(DataTable))), DataTable)
    For Each trow As DataRow In t.Rows
        MsgBox(trow("LineId"))
    Next
    Return ""
End Function

我正在使用JSON.net并且反序列化似乎不起作用,当我将函数参数声明为字符串时,它会抛出此错误&#34;消息:类型System.String不支持反序列化数组。 ...&#34; 但是当我使用object时,它会抛出一个不能用于数据表的错误。

<WebMethod()> _
Public Function invExecInvLines(ByVal data As object)
    Dim t As DataTable = DirectCast(JsonConvert.DeserializeObject(data, (GetType(DataTable))), DataTable)
    For Each trow As DataRow In t.Rows
        MsgBox(trow("LineId"))
    Next
    Return ""
End Function

谢谢!

1 个答案:

答案 0 :(得分:0)

我没有将变量作为对象传递,而是使用足以满足此要求的通用列表。 添加接口定义和通用集合。