Home > Mobile >  VB.NET Json request
VB.NET Json request

Time:12-02

Having some trouble trying to convert my json data to something usable

my json data

{
  "4": {
    "name": "Warehouse",
    "tenantcode": "242",
    "package_id": 1,
    "package": "package10",
    "ext_length": 4,
    "country_id": 91,
    "country_code": 61
  },
  "5": {
    "name": "Partners",
    "tenantcode": "240",
    "package_id": 1,
    "package": "package10",
    "ext_length": 4,
    "country_id": 91,
    "country_code": 61
  },
  "8": {
    "name": "Systems",
    "tenantcode": "241",
    "package_id": 20,
    "package": "Systems",
    "ext_length": 4,
    "country_id": 91,
    "country_code": 61
  },

VB code

Imports System.Net
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq



Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim json2 As String = (New WebClient).DownloadString("https://myapi")
        Dim json As String = json2

        Dim ser As JObject = JObject.Parse(json)
        Dim data As List(Of JToken) = ser.Children().ToList

        For Each item As JProperty In data
            item.CreateReader()
            Select Case item.Name

                Case "Name"

this is a far as i got.

i need to know whats the best was to try and use my json data. build a new class?

CodePudding user response:

Yes, you should build a new class. The data you provided can be deserialized as a Dictionary, so you'd just have to make a class for the details.

Public Class Details
    Public Property Name As String
    Public Property Tenantcode As String
    Public Property Package_id As Integer
    Public Property Package As String
    Public Property Ext_length As Integer
    Public Property Country_id As Integer
    Public Property Country_code As Integer
End Class

Then simply deserialize your data into a Dictionary(String, Details)

Dim data = JsonConvert.DeserializeObject(Of Dictionary(Of String, Details))

CodePudding user response:

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 _4 As _4
    Public Property _5 As _5
End Class

Public Class _4
    Public Property name As String
    Public Property tenantcode As String
    Public Property package_id As Integer
    Public Property package As String
    Public Property ext_length As Integer
    Public Property country_id As Integer
    Public Property country_code As Integer
End Class

Public Class _5
    Public Property name As String
    Public Property tenantcode As String
    Public Property package_id As Integer
    Public Property package As String
    Public Property ext_length As Integer
    Public Property country_id As Integer
    Public Property country_code As Integer
End Class

Since the Rootobject is basically acting like an associative array (in PHP) or dictionary (in .NET), then what I would do is get rid of Rootobject and _4, and rename _5 to be something a little more generic. Also, you can use decorators to make the property names conform to a more .NET style if you wanted:

Public Class Record

    <JsonProperty("name")>
    Public Property Name As String

    <JsonProperty("tenantcode")>
    Public Property TenantCode As String

    <JsonProperty("package_id")>
    Public Property PackageId As Integer

    <JsonProperty("package")>
    Public Property Package As String

    <JsonProperty("ext_length")>
    Public Property ExtLength As Integer

    <JsonProperty("country_id")>
    Public Property CountryId As Integer

    <JsonProperty("country_code")>
    Public Property CountryCode As Integer

End Class

Now what you'd do is create your webclient, download the string, and use DeserializeObject to convert the associative array to a dictionary:

Dim records As Dictionary(Of Integer, Record)
Using client As New WebClient()
    Dim payload As String = client.DownloadString("https://myapi")
    records = JsonConvert.DeserializeObject(Of Dictionary(Of Integer, Record))(payload)
End Using

Now you can get the object by it's key and access it's properties. E.g.:

If (records.ContainsKey(4)) Then
    Console.WriteLine(records(4).Name)
End If
  • Related