I am trying to figure out a class/struture to handle the following JSON format:
{
"ReturnData": [
{
"id": "msg2DoesNotExistName",
"value": "value 1",
"userExists": 2
},
{
"id": "msg2DoesNotExistName",
"value": "Value 2",
"userExists": 2
}
],
"SetValue": [
{
"id": "msg2DoesNotExistName",
"value": "value 1"
},
{
"id": "msg2DoesNotExistName",
"value": "Value 2"
}
]
}
I have tried (just the SetValue portion):
<Serializable()> _
Public Class Stuff
Public SetValue() As ArrayList
End Class
Public Function TestSerial3(ByVal somevar As String) As String
Dim s As New JavaScriptSerializer()
Dim ret As String
Dim b As New SaveType()
Dim p1 As New Stuff
b = New SaveType
b.id = "ctl00_number_1"
b.value = "Test1"
p1.SetValue(0).Add(b)
b = New SaveType
b.id = "ctl100_number_2"
b.value = "Test2"
p1.SetValue(1).Add(b)
ret = s.Serialize(p1)
return ret
end function
This is the result: System.NullReferenceException: Object reference not set to an instance of an object.
I am able to serialize the inside portion using a structure, but cannot figure out how to include the outer name (ReturnData, SetValue) without resorting to string building:
<Serializable()> _
Public Structure UserExistsType
Public id As String
Public value As String
Public userExists As Integer
End Structure
Dim b(1) As UserExistsType
b(0).id = "msg2DoesNotExistName"
b(0).value = "value 1"
b(0).userExists = 2
b(1).id = "msg2DoesNotExistName"
b(1).value = "Value 2"
b(1).userExists = 2
ret = s.Serialize(b)
ret = "{" & Chr(34) & "ReturnData" & Chr(34) & ":" & ret & "}"
I may or may not have Data for ReturnData and SetValue (one or both at a minimum). I am trying to let the serializer handle most of the formatting without having to check for empty sections and single-item arrays. Any suggestions?