Bill Blankenship
Bill Blankenship

Reputation: 3356

How to create a VB.net class from this JSON?

I am attempting to create a class in vb.net from the below JSON example.

I am fairly new to JSON, and I am just having trouble figuring out the correct way of doing this. I have looked at numerous examples of how to do this but they are in much simpler formats than the one I am providing below.

LEVEL0({
  "LEVEL1": [
    {
      "LEVEL2": [
        {
          "City": "Billings",
          "State": "MO",
          "Country": "US",
          "Id": "1122334455",
          "Percent": "39.10"
          }
      ],
      "City": "Billings",
      "Country": "US",
      "NumFound": "1",
      "NumReturned": "1",
      "State": "MO",
      "Status": "Success"
    }
  ],
  "Status": "1"
});

I was thinking that this would be fairly easy, but what is causing me issues is that LEVEL2 is a list. It could return back multiple cities. So, it could return something like this within the [].

{"City": "Billings","State": "MO","Country": "US","Id": "1122334455","Percent": "39.10"},
{"City": "Fairmount","State": "MN","Country": "US","Id": "1177775","Percent": "64.10",}

So either way that portion needs to be a list, but then the portion below the list needs to be part of that class also. Where you see numFound.

I am guessing this is poorly worded, but I am struggling to find a good example of how this would be done. Any direction or advice on this would be greatly appreciated. I need to have the class correctly formatted so when I deserialize into the class it works without error.

Upvotes: 1

Views: 1065

Answers (1)

Jason
Jason

Reputation: 52523

If you're using a webservice of some sort, it should automatically bind for you if you are using an object that has all of these properties. Something like:

Public Class WrapperClass
    Dim Status As Integer
    Dim LEVEL1 As New List(Of MiddleClass)
End Class

Public Class MiddleClass
    Dim LEVEL2 As New List(Of InnerClass)
    Dim City As String
    Dim Country As String
    Dim NumFound As Integer
    Dim NumReturned As Integer
    Dim State As String
    Dim Status As String

    Public Sub MiddleClass()
    'init code
    End Sub
End Class

Public Class InnerClass
    Dim City As String
    Dim Country As String
    Dim State As String
    Dim Id As Integer
    Dim Percent As Decimal
End Class

Then in your webservice, have it accept a parameter of type WrapperClass

Upvotes: 2

Related Questions