Reputation: 857
I've been racking my brain for the past few hours.
I found that you can serialize the data and as such save the hassle of creating endlessly long .split
commands for each of the seperate JSON values. So I've followed what a lot of people on SO and all over the web have done, by creating a JsonHelper class to Serialize and Deserialize the JSON that is passed from an ASP.net page.
This is my class:
Public Class NewTaskOb
Public Property Department As Integer
Public Property Type As Integer
Public Property AssignedTo As Integer
Public Property Priority As Integer
Public Property Title As String
Public Property JobCode As Integer
Public Property ParentTask As String
Public Property DueDate As Date
Public Property Scope As String
Public Property Comment As String
Public Property BudgetString As String
End Class
Then I have this:
<WebMethod()> Public Shared Function NewTaskRequest(task As String) As String
Dim newTask As NewTaskOb = DeserializeJSON.JsonDeserialize(Of NewTaskOb)(task)
My JSON is valid according to jsonlint, but when I pass the values through, I check by putting a breaker on the Dim newTask
line, and when I hover my mouse over it, it says each of the values is empty (Department = 0, Type = 0, etc.) when, on the form, I am submitting values.
I have tried by using DataContractJsonSerializer
, JsonHelper
, and JavaScriptSerializer
. Please note, I do not want to import another reference (i.e Newtonsoft.Json
, or anything similar). I want to get this done using the aforementioned functions.
Thanks for your help in advance.
Edit (adding in the JSON string itself):
{
"NewTask": {
"Department": "-1",
"Type": "0",
"AssignedTo": "0",
"Priority": "-1",
"Title": "",
"JobCode": "-",
"ParentTask": "-",
"DueDate": "",
"Scope": "",
"Comment": "",
"BudgetString": ""
}
}
Edit:
While messing around with the code, I went back to the JavaScriptSerializer
and found if I put Dim data = jss.Deserialize(Of Object)(task)
in place of Dim data = jss.Deserialize(Of NewTaskOb)(task)
, then it shows up while stepping through that there is a dictionary in the data, that has the fields with the data.
Dictionary showing JSON correctly http://imageshack.com/a/img401/1174/a9ju.png
Any idea how I can extract these into the class variables?
Thanks in advance.
Upvotes: 1
Views: 3070
Reputation: 659
So I was testing your program on my system and noticed that it has Dictionary"2. Imagining what it could possibly mean, I had the idea that 2 could mean there is a dictionary nested within a dictionary - which could explain why you couldn't retrieve the values. So I edited away, and ended up with this line - Dim taskData = jss.Deserialize(Of Dictionary(Of Object, Object))(task)
. Now, when I run it I have put in Dim taskValues = taskData.Values
, and I can reference each of the values using their respective index positions...
i.e taskValues(0) = Department, taskValues(1) = Type, etc.
Also, I removed the line where you were inserting the data into a Data Transfer Object, thus eliminating the "NewTask":
nesting within the JSON, resulting in this JSON:
{
"Department": "-1",
"Type": "0",
"AssignedTo": "0",
"Priority": "-1",
"Title": "",
"JobCode": "-",
"ParentTask": "-",
"DueDate": "",
"Scope": "",
"Comment": "",
"BudgetString": ""
}
Upvotes: 1