I have a JSON object that looks like this.
{
"Errors":{
"err1":[
//* Array of err1 objects
],
"err2":[
//* Array of err2 objects
]
}
}
I use this object to report errors found on a request to a PHP page.
I want to convert that object to a vb.Net object declared basically like this:
Public Class WebErrContainer
Public Errors as List(Of IWebError)
End Class
Public Class Err1
Implements IWebError
End Class
Public Class Err2
Implements IWebError
End Class
Public Interface IWebError
End Interface
I don't know if my implementation is good enough, i'm relatively new to vb.net so my skills in OOP are a little low right now.
That's it... i think i've explained the situation as well as i could.
If you need more information, i will give it .
Thanks in advance for any help that you could give. Thanks.
PD: I'm currently using Newtonsoft's JSON.Net library. Feel free to recommend some other library or method to deal with JSON in VB.NET.
I solved the situation by doing this:
Public Sub New(ByVal jsonText As String)
Dim jObject As JObject = jObject.Parse(jsonText )
Dim jErrors As JToken = jObject("Errors")
Dim jS = New JsonSerializer()
' This is the "temporal" dictionary that stores the errors like they are
' stored in JSON
Dim jsErrs As New Dictionary(Of String, List(Of Object))
' List that is going to be passed to the "Errors" field in the class
Dim lErrors As New List(Of IWebError)
jsErrs = jS.Deserialize(New JTokenReader(jErrors ), jsErrs .GetType)
If Not jsErrs Is Nothing Then
For Each errType As KeyValuePair(Of String, List(Of Object)) In jsErrs
For Each Err As Object In errType.Value
lErrors .Add(jS.Deserialize(New JTokenReader(Err), Type.GetType(errType.Key)))
Next
Next
End If
Me.Errors = lErrors
End Sub