Reputation: 147
I have a JSON object built in javascript that I would like to pass to an ajax request into a VB.Net WebMethod and deserialize it. I am using Newtonsoft JSON to deserialize the object. It seems like the object is passed over but when I try to deserialize it into an object I get this error : Error converting value "myid" to type 'AnID.RF.MyIDProfile'.
Here is the javascript code I am using:
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);
}
})
}
Here is the server side code:
<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
If I use SelectToken("profile.myid") I am able to get the value with no issues. I am not sure what I could be doing incorrectly that is causing this error. Can anyone take a look and see?
Upvotes: 2
Views: 710
Reputation: 147
I figured out what was wrong. The code I had was for deserializing an array of objects and the json object passed through was not an array of objects of which caused the error. I removed the loop and replaced it like so:
<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
Upvotes: 1