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:

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

Open in new window


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

Open in new window


When I step through and check the values of qv and mess they are "Nothing."

Any ideas what I'm doing wrong?

thanks!
LVL 2
Starr DuskkASP.NET VB.NET DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_TAD_Commented:
I don't think you can deserialize something that exists only in memory.

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
0
käµfm³d 👽Commented:
I don't think you can deserialize something that exists only in memory.
You most certainly can. A stream is a stream as far as serializers are concerned. Streams are just abstractions of data stores, so memory vs. file is inconsequential.

As for the issue at hand, the "new" javascript structure doesn't match the class you are trying to deserialize to. Put a breakpoint on line 7, then mouse over the variable serializedResult--it should be easy to spot the difference in structure. The structure of JSON that you see in serializedResult is what you need to mimic in line 7 when you hard-code the data.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
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","ErrorMessage":"My Mess"}
But there is no class name, so Line 7 I add the class name, which returns this:
{"Test2Properties": {"QuestionValue":"Test2","ErrorMessage":"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!
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
Also, Tad, your code is in java and is missing some of the methods called. I can't use it, but thanks for trying!
0
käµfm³d 👽Commented:
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.

Screenshot
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.
0
käµfm³d 👽Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.