从AJAX请求中反序列化Webmethod中的JSON
Deserialize JSON in Webmethod from AJAX request
我有一个javascript内置的JSON对象,我想传递到一个ajax请求到VB。Net WebMethod并对其进行反序列化。我使用Newtonsoft JSON来反序列化对象。对象似乎被传递了但当我试图将其反序列化为对象时,我得到了这个错误:Error converting value "myid" to type 'AnID.RF.MyIDProfile'.
下面是我使用的javascript代码:
function test() {
var testdata;
testdata = "{'data':{'ses':'','profile':{'myid':'myid','username':'User','firstName':'adf','lastName':'lastname','languagePreference':'en'}}}";
$.ajax({
type: "POST",
url: "default.aspx/Foo",
data: testdata,
contentType: "application/json; charset=utf-8",
dataType: "json",
beforeSend: function() {
},
success: function(data) {
if (data != "") {
alert(data.d);
}
},
error: function(jqXHR, textStatus, errorThrown) {
alert(errorThrown);
}
})
}
服务器端代码:
<System.Web.Services.WebMethod()> _
Public Shared Function Foo(ByVal data As Object) As String
Dim jO As JObject = JObject.FromObject(data)
Dim sMessage As String = "y"
Try
Dim results As IList(Of JToken) = jO("profile").Children().ToList()
Dim searchResults As IList(Of RF.MyIDProfile) = New List(Of RF.MyIDProfile)()
For Each result As JToken In results
Dim searchResult As RF.MyIDProfile = JsonConvert.DeserializeObject(Of RF.MyIDProfile)(result.ToString())
searchResults.Add(searchResult)
Next
Dim stest As String = jO.SelectToken("profile.myid").ToString
Catch ex As Exception
sMessage = "e"
End Try
Return sMessage
End Function
Namespace RF
Public Class MyIDProfile
Private sMYID As String
Private sUSERName As String
Private sPrefix As String
Private sFirstName As String
Private sLastName As String
Private slanguagePreference As String
Public Property myid() As String
Get
Return sMYID
End Get
Set(ByVal value As String)
sMYID = value
End Set
End Property
Public Property username() As String
Get
Return sUSERName
End Get
Set(ByVal value As String)
sUSERName = value
End Set
End Property
Public Property firstName() As String
Get
Return sFirstName
End Get
Set(ByVal value As String)
sFirstName = value
End Set
End Property
Public Property lastName() As String
Get
Return sLastName
End Get
Set(ByVal value As String)
sLastName = value
End Set
End Property
Public Property languagePreference() As String
Get
Return slanguagePreference
End Get
Set(ByVal value As String)
slanguagePreference = value
End Set
End Property
End Class
End Namespace
如果我使用SelectToken("profile.myid"),我能够获得没有问题的值。我不确定我可能做错了什么,导致这个错误。有人能看一下吗?
我明白是怎么回事了。我所拥有的代码是用于反序列化对象数组,并且传递的json对象不是导致错误的对象数组。我删除了循环,并将其替换为如下所示:
<System.Web.Services.WebMethod()> _
Public Shared Function Foo(ByVal data As Object) As String
Dim jO As JObject = JObject.FromObject(data)
Dim sMessage As String = "y"
Try
Dim oDetail As RF.MyIDProfile = jO("profile").ToObject(Of RF.MyIDProfile)()
Dim sTest2 As String = oDetail.myid
Catch ex As Exception
sMessage = "e"
End Try
Return sMessage
End Function
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 可以't让我的if语句处理js中的html表单输入
- 引用对象中的通用值
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 用程序搜索JQuery数据表中的文本
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- 不能从angular2中的子组件指定父组件中的数组
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- Facebook共享按钮不;在使用Javascript和Ajax的Webmethod中似乎不起作用
- 通过 aspx 网页中的 Angularjs 将数据传递给 WebMethod
- 如何从 vb.net 中的 ASPX.vb 页调用 WebMethod
- Asp.net中的Ajax调用([WeBMethod]函数未调用)
- 从Usercontrol调用Usercontrol.cs中的Webmethod.ascx javascript
- 从AJAX请求中反序列化Webmethod中的JSON
- 将查询字符串传递给Javascript中的WebMethod
- from webmethod返回了javascript中的二维数组
- PageMethods没有定义异常:不调用ASP.NET中的WebMethod
- 从JavaScript WebMethod中访问单独的c#文件