Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27360

[RESOLVED] Extracting JSON data

$
0
0
Hey guys, it's been a few years since I've touched VB.net, and I'm needing a bit of help with a project that I'm working on for a family member. I'm writing a program to manage and maintain an auto repair shop, and I'm having you get a massive list of all vehicles available for entry into the database.

I stumbled across a free API online that delivers all the information I need in JSON format. Currently, I'm using JSON.net to attempt to parse the information, but since I've never worked with JSON, I'm somewhat lost. My current method is sloppy and ugly, and I'm sure there is another method to do what I need. All I need to do is essentially get every single vehicle then enter it into a database for later usage. The raw JSON data I'm using can be located here - http://pastebin.com/av1nx9JE

My current method is essentially a very complicated loop using JSON.net to read each object and if they match a set of constraints, then it adds it to a variable, once it reaches the end of a specific path, it enters that into the database. It's very ugly and slow.

JSON.net allows for deserialization, but I'm unsure of how to use this. Any information would be extremely helpful.




EDIT -----
Hey guys, after some more tinkering, digging, and experimenting, I've figured it out.
Here's the original method I was using. It's very sloppy as I was simply trying to get something working.
Code:

        Dim idA As Integer = 0, idB As Integer = 0, idC As Integer = 0
        Dim storeVal As Boolean = False, endPath As Boolean
        Dim makeName As String, makeID As String, makeNiceName As String, modelID As String, modelName As String, modelNiceName As String, yearID As String, year As String

        Dim reader As New JsonTextReader(New StringReader(TextBox2.Text))
        While reader.Read()
            If (reader.Value <> vbNullString) Then
                If reader.TokenType.ToString = "PropertyName" Then
                    storeVal = True
                Else
                    If storeVal = True Then
                        If reader.Path = "makes[" & idA & "].name" Then
                            makeName = reader.Value
                        ElseIf reader.Path = "makes[" & idA & "].id" Then
                            makeID = reader.Value
                            idB = 0
                            idC = 0
                        ElseIf reader.Path = "makes[" & idA & "].niceName" Then
                            makeNiceName = reader.Value
                            idB = 0
                            idC = 0
                        ElseIf reader.Path = "makes[" & idA & "].models[" & idB & "].id" Then
                            modelID = reader.Value
                            idC = 0
                        ElseIf reader.Path = "makes[" & idA & "].models[" & idB & "].name" Then
                            modelName = reader.Value
                            idC = 0
                        ElseIf reader.Path = "makes[" & idA & "].models[" & idB & "].niceName" Then
                            modelNiceName = reader.Value
                            idC = 0
                        ElseIf reader.Path = "makes[" & idA & "].models[" & idB & "].years[" & idC & "].id" Then
                            yearID = reader.Value
                        ElseIf reader.Path = "makes[" & idA & "].models[" & idB & "].years[" & idC & "].year" Then
                            year = reader.Value
                            idC += 1
                            endPath = True
                            ListView1.Items.Add(makeName)
                            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(modelName)
                            ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(year)
                        ElseIf reader.Path = "makes[" & (idA + 1) & "].id" Then
                            idA += 1
                            idB = 0
                            idC = 0
                        ElseIf reader.Path = "makes[" & idA & "].models[" & (idB + 1) & "].id" Then
                            idB += 1
                        End If
                    End If
                End If
            End If
        End While


And here's the corrected method. For some reference, the above method took nearly a minute to fully execute, the new one only takes about 3 seconds.
Code:

        Dim des As RootObject = JsonConvert.DeserializeObject(Of RootObject)(TextBox2.Text)

        For i = 0 To des.makesCount - 1
            For x = 0 To des.makes(i).models.Count - 1
                For y = 0 To des.makes(i).models(x).years.Count - 1
                    ListView1.Items.Add(des.makes(i).name)
                    ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(des.makes(i).models(x).name)
                    ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(des.makes(i).models(x).years(y).year)
                    ListView1.EnsureVisible(ListView1.Items.Count - 1)
                Next
            Next
        Next

Also, to get the above working, the following classes were necessary.
Code:

    Public Class Year
        Public Property id() As Integer
            Get
                Return m_id
            End Get
            Set(value As Integer)
                m_id = value
            End Set
        End Property
        Private m_id As Integer
        Public Property year() As Integer
            Get
                Return m_year
            End Get
            Set(value As Integer)
                m_year = value
            End Set
        End Property
        Private m_year As Integer
    End Class

    Public Class Model
        Public Property id() As String
            Get
                Return m_id
            End Get
            Set(value As String)
                m_id = value
            End Set
        End Property
        Private m_id As String
        Public Property name() As String
            Get
                Return m_name
            End Get
            Set(value As String)
                m_name = value
            End Set
        End Property
        Private m_name As String
        Public Property niceName() As String
            Get
                Return m_niceName
            End Get
            Set(value As String)
                m_niceName = value
            End Set
        End Property
        Private m_niceName As String
        Public Property years() As List(Of Year)
            Get
                Return m_years
            End Get
            Set(value As List(Of Year))
                m_years = value
            End Set
        End Property
        Private m_years As List(Of Year)
    End Class

    Public Class Make
        Public Property id() As Integer
            Get
                Return m_id
            End Get
            Set(value As Integer)
                m_id = value
            End Set
        End Property
        Private m_id As Integer
        Public Property name() As String
            Get
                Return m_name
            End Get
            Set(value As String)
                m_name = value
            End Set
        End Property
        Private m_name As String
        Public Property niceName() As String
            Get
                Return m_niceName
            End Get
            Set(value As String)
                m_niceName = value
            End Set
        End Property
        Private m_niceName As String
        Public Property models() As List(Of Model)
            Get
                Return m_models
            End Get
            Set(value As List(Of Model))
                m_models = value
            End Set
        End Property
        Private m_models As List(Of Model)
    End Class

    Public Class RootObject
        Public Property makes() As List(Of Make)
            Get
                Return m_makes
            End Get
            Set(value As List(Of Make))
                m_makes = value
            End Set
        End Property
        Private m_makes As List(Of Make)
        Public Property makesCount() As Integer
            Get
                Return m_makesCount
            End Get
            Set(value As Integer)
                m_makesCount = value
            End Set
        End Property
        Private m_makesCount As Integer
    End Class


Viewing all articles
Browse latest Browse all 27360

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>