Starr Duskk
asked on
JavascriptSerializer - deserializing woes
I use the javascriptserializer all the time, but this is the first time I've tried to use the deserialize. Every test I've run, the properties come up as "nothing."
Here is my test class:
Here is my test code:
When I step through and check the values of qv and mess they are "Nothing."
Any ideas what I'm doing wrong?
thanks!
Here is my test class:
Public Class Test2Properties
Private _QuestionValue As String
Public Property QuestionValue() As String
Get
Return _QuestionValue
End Get
Set(ByVal value As String)
_QuestionValue = value
End Set
End Property
Private _ErrorMessage As String
Public Property ErrorMessage() As String
Get
Return _ErrorMessage
End Get
Set(ByVal value As String)
_ErrorMessage = value
End Set
End Property
End Class
Here is my test code:
Dim ji As New Test2Properties
ji.QuestionValue = "Test2"
ji.ErrorMessage = "My Mess"
Dim serializer As New JavaScriptSerializer()
Dim serializedResult = serializer.Serialize(ji)
serializedResult = "{""Test2Properties"": " & serializedResult & "}"
Dim jss As New JavaScriptSerializer()
Dim sd As Test2Properties = jss.Deserialize(Of Test2Properties)(serializedResult)
Dim qv As String = sd.QuestionValue
Dim mess As String = sd.ErrorMessage
When I step through and check the values of qv and mess they are "Nothing."
Any ideas what I'm doing wrong?
thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Kaufmed,
Sorry, but I'm confused.
I'm starting out with the Test2Properties class. Serializing it. Then I want to take that string value and pull out the properties again.
Line 6 returns this:
{"QuestionValue":"Test2"," ErrorMessa ge":"My Mess"}
But there is no class name, so Line 7 I add the class name, which returns this:
{"Test2Properties": {"QuestionValue":"Test2"," ErrorMessa ge":"My Mess"}}
You said:
>>The structure of JSON that you see in serializedResult is what you need to mimic in line 7 when you hard-code the data.
What am I not understanding? How should I mimic it? As far as I can see, I *am* mimicking it?
thanks!
Sorry, but I'm confused.
I'm starting out with the Test2Properties class. Serializing it. Then I want to take that string value and pull out the properties again.
Line 6 returns this:
{"QuestionValue":"Test2","
But there is no class name, so Line 7 I add the class name, which returns this:
{"Test2Properties": {"QuestionValue":"Test2","
You said:
>>The structure of JSON that you see in serializedResult is what you need to mimic in line 7 when you hard-code the data.
What am I not understanding? How should I mimic it? As far as I can see, I *am* mimicking it?
thanks!
ASKER
Also, Tad, your code is in java and is missing some of the methods called. I can't use it, but thanks for trying!
But there is no class name, so Line 7 I add the class name, which returns this:That is correct: there is no class name. You don't need it (in the JSON), so you shouldn't be adding it.
As far as I can see, I *am* mimicking it?But you're not. Paste each into this formatter: http://www.freeformatter.com/json-formatter.html. What's different?
e.g.
You've added an additional layer with the "Test2Properties" addition, and that extra layer is breaking your deserialization. Removing that layer should make your deserialization work.
P.S.
Keep in mind that the serializer is going to spit out whatever structure it would expect to receive back. The output that you got back from serialization shows the structure that you would need to pass back during deserialization.
Keep in mind that the serializer is going to spit out whatever structure it would expect to receive back. The output that you got back from serialization shows the structure that you would need to pass back during deserialization.
Try writing your serialized data out to a file, and then deserialize that file.
Here is a link to some code that works
http://code.runnable.com/UpvRGuS4e6UcAAED/how-to-deserialize-objects-in-java-for-deserialization