0

I'm having a problem with JSON when I try to deserialize to a class. My current code

    Public Class Client

        Public Property status As Boolean
        Public Property msg As String
        Public Property data As String

    End Class

And I am using the following code to deserialize

JavaScriptSerializer().Deserialize(Of Client)(input)

The problem is that I can only use the public property, if I set the property to READONLY or PRIVATE SET, then the status/msg/data are not set.

I have been tried

        Private _status As Boolean
        Public Property status As Boolean
            Get
                Return _status
            End Get
            Private Set(value As Boolean)
                _status = value
            End Set
        End Property

AND

Public ReadOnly Property status As Boolean

Both will return nothing.

Would like to know if there is any way that I can make the property read-only? I am using public property right now, but I really don't want the value can be altered.

Any help will be greatly appreciated.

Thanks.

  • For PRIVATE SET, assuming you can modify the class, you can apply `` to the property as shown in [JSON Serializer object with internal properties](https://stackoverflow.com/q/26873755/3744182). If READONLY, you need to deserialize using a parameterized constructor as shown in [Avoiding default constructors and public property setters](https://stackoverflow.com/q/35245256/3744182) (which also works for properties with a private setter). Those answers are for c#, do they answer your question, or do you need a vb-specific answer? – dbc Jan 15 '20 at 17:58
  • You can also use `Public ReadOnly` fields (instead of properties) decorated with ``, without any *specialized* constructor. – Jimi Jan 15 '20 at 18:04

0 Answers0