Deserialize using JSON.net
Your classes are pretty close, it looks like you possibly tried to pretty things up a bit such as changing codes
to Codes
but in so doing the properties no longer match. You can change class names but not property names (at least not that way):
Public Class CodeLinkContainer
<JsonProperty("codes")>
Public Property Codes As IList(Of Code)
<JsonProperty("links")>
Public Property Links As IList(Of Link)
<JsonProperty("meta")>
Public Property Meta As Meta
End Class
Public Class Meta
Public Property description As Object
Public Property last_page As Integer
Public Property page_offset As Integer
Public Property page_size As Integer
Public Property querytemplate As String
Public Property total As Integer
End Class
Public Class Code
Public Property href As String
Public Property rel As String
Public Property title As String
End Class
Public Class Link
Public Property about As String
Public Property href As String
Public Property method As String
Public Property rel As String
Public Property title As String
Public Property type As String
End Class
Using AutoImplement properties, available for some time now, means you can skip all the Get
, Set
boilerplate code. VS will create the classes for you also:
Edit Menu -> Paste Special -> Paste Json As Classes
You sometimes have to tweak the class if there is an array/list property. For instance, the robots may write:
Public Property elements() As Element
When it should be:
Public Property elements As Element()
The container class shows how to use <JsonProperty("pname")>
to change the property name if you wish. This often needs to be done to create an alias for a property name which is a key word in VB (Return
, Error
etc). In this case, I changed codes
and links
to be Lists
as you did.
Dim jstr = ... from whereever
Dim CodeLinks = JsonConvert.DeserializeObject(Of CodeLinkContainer)(jstr)
Console.WriteLine(CodeLinks.meta.total)
For Each Item In CodeLinks.codes
Console.WriteLine(Item.title)
Next
Result:
6
TITLE 1
TITLE 2
TITLE 3