Search code examples
jsonvb.netjson-deserialization

JSON - Conversion to VB.NET Object


I am new to JSON strings so I am having a hard time with it. I am used to SOAP Web Services where in Visual Studio automatically creates the strongly typed Classes for me in the background.

JSON - REST Web Services are for me is much tedious as I have to create manually the equivalent Strongly Typed Classes. (Or I maybe wrong).

I have this JSON String being returned to me by a REST Web Service:

{"Message":"The request is invalid.","ModelState":{"command":["Required property 'Vendor' not found in JSON. Path '', line 1, position 310."],"command.Terms":["The Terms field is required."]}}

Could you guide me on the equivalent Class?

Appreciate your help!

Regards, Jake


Solution

  • assuming you are using JSON.NET...

    Public Class Response
        Public Property Message As String
        Public Property ModelState As ModelState
    End Class
    
    Public Class ModelState
        <JsonProperty("command")>
        Public Property Command As List(Of String) = New List(Of String)
    
        <JsonProperty("command.Terms")>
        Public Property Terms As List(Of String) = New List(Of String)
    End Class
    

    Usage:

    Dim response As Response = JsonConvert.DeserializeObject(jsonString)