0

This should be straightforward but I can't figure it out.

I have a simple JSON array: [{"carId":1,"status":"Active","inventory":"3"}, {"carId":2,"status":"Active","inventory":"5"}]

I have my JSON classes:

Friend Class carModel
    Public Property carId As String
    Public Property status As String
    Public Property inventory As String
End Class

Friend Class carWrapper
    Friend arrcarModel() As carModel
End Class

And I have the following code:

Dim currentWrapper = DeserializeObject(Of List(Of carWrapper))(strJSON)

If currentWrapper.Count > 0 Then
    For i As Integer = 0 To RXWrapper.Count - 1
        Dim carItem = currentWrapper(i).arrcarModel
    Next
End If

Although the code does indeed result with currentWrapper.Count = 2, currentWrapper(i).arrcarModel always winds up equal to Nothing.

What am I not doing correctly? Is there a more standard approach to deserializing a JSON array?

Thanks!

3
  • 2
    There's no Root object there. You have just an array of carModel objects. I.e., you don't need carWrapper. Commented Aug 11, 2021 at 15:56
  • Hey Jimi. You're right that there's a root object missing. Thanks for that. But I don't know how to process a JSON array (as opposed to a simple JSON string) without a wrapper... Commented Aug 11, 2021 at 20:10
  • Dim cardModels = JsonConvert.DeserializeObject(Of List(Of CarModel))(strJSON). Commented Aug 11, 2021 at 21:32

1 Answer 1

1

Visual Studio has a cool feature called Paste JSON as Classes that can be found under Edit > Paste Special > Paste JSON as Classes. If you were to do this, then you would get something that looks like the following:

Public Class Rootobject
    Public Property Property1() As Class1
End Class

Public Class Class1
    Public Property carId As Integer
    Public Property status As String
    Public Property inventory As String
End Class

Personally, I would clean up the class a little bit by doing some renaming and using decorators. For example:

Public Class CarWrapper

    Public Property CarModels As IEnumerable(Of CarModel)

End Class

Public Class CarModel

    <JsonProperty("carId")>
    Public Property CarId As Integer

    <JsonProperty("status")>
    Public Property Status As String

    <JsonProperty("inventory")>
    Public Property Inventory As String

End Class

Now what you would do is create a new instance of the CarWrapper class and set the CarModels property equal to the deserialization of the JSON:

Dim json = <json>
            [
                {
                    "carId": 1,
                    "status": "Active",
                    "inventory": "3"
                },
                {
                    "carId": 2,
                    "status": "Active",
                    "inventory": "5"
                }
            ]
            </json>.Value
Dim wrapper = New CarWrapper() With {
    .CarModels = JsonConvert.DeserializeObject(Of CarModel())(json)
}

For Each model In wrapper.CarModels
    Console.WriteLine("Id: {0}, Status: {1}, Inventory: {2}", model.CarId, model.Status, model.Inventory)
Next

Sign up to request clarification or add additional context in comments.

2 Comments

Hey David. This works perfectly! Thanks for taking the time to lay out the details of the solution. Greatly appreciated!!!
@Mike - No problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.