none
Deserializing JSON RRS feed

  • Question

  • I am attempting to use JSON.NET to deserialize a string and just can't get it to work.  I have been staring at this all day and checking every resource that I can find.  I am not having any success and am hoping that someone on this forum can help me.

    Here is my data:

    {"list":[{"employeeId":1,"departmentId":4,"lastChanged":"2014-04-24 10:56:05","number":0,"uniqueId":"","firstName":"Jack","lastName":"Ryan","departments":[{"departmentId":3,"member":true},{"departmentId":4,"member":false},{"departmentId":5,"member":true}]},
    {"employeeId":2,"departmentId":11,"lastChanged":"2014-04-23 11:17:01","number":12,"uniqueId":"","firstName":"Scott","lastName":"Smithers","departments":[{"departmentId":3,"member":false},{"departmentId":4,"member":true},{"departmentId":5,"member":true}]},]}

    Here is my class for the data:

        Public Class list
            Public employeeID As Integer
            Public departmentID As Integer
            Public lastChanged As String
            Public number As Integer
            Public displayName As String
            Public firstName As String
            Public lastName As String
            Public Property Departments() As Department

        End Class

        Public Class Department
            Public departmentID As Integer
            Public Member As Boolean
        End Class

    Here is the code that I am using to deserialize the data:

            Dim obj As JSON.Employee
            obj = JsonConvert.DeserializeObject(Of JSON.Employee)(sResult)
    
            Debug.WriteLine("JSON: " + obj.firstName)

    The code executes fine but the result of the debug statement is blank.

    First, what am I doing wrong here?

    Second, how do I parse through the deserialized object to see my data?

    Thanks for your help.

    John


    John

    Monday, May 5, 2014 10:39 PM

Answers

  • Hi John,

    I don't afraid your code executes fine, you didn't catch the exception.

    Please check the whole code below: I changed the array definination to List, and I parse the data good.

    Imports Newtonsoft.Json
    
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
            Dim sResult As String = FileIO.FileSystem.ReadAllText(".\data.txt")
            Dim obj As ListEmployee
            obj = JsonConvert.DeserializeObject(Of ListEmployee)(sResult)
    
            Console.WriteLine("JSON: " + obj.list(0).firstName)
    
        End Sub
    End Class
    
    Public Class ListEmployee
        Public list As List(Of Employee)
    End Class
    
    Public Class Employee
        Public employeeID As Integer
        Public departmentID As Integer
        Public lastChanged As String
        Public number As Integer
        Public displayName As String
        Public firstName As String
        Public lastName As String
        Public Property Departments As List(Of Department)
    End Class
    
    Public Class Department
        Public departmentID As Integer
        Public Member As Boolean
    End Class

    I hope this will be helpful.


    Mike Feng
    Help me, help you

    • Proposed as answer by Carl CaiModerator Tuesday, May 6, 2014 3:36 AM
    • Marked as answer by Johngeh Tuesday, May 6, 2014 7:46 PM
    Tuesday, May 6, 2014 3:12 AM
    Moderator
  • Hi, I think you need to change Public Property Departments() As Department to Public Departments() As Department.

    please see below code:

        Public Class list
            Public employeeID As Integer
            Public departmentID As Integer
            Public lastChanged As String
            Public number As Integer
            Public displayName As String
            Public firstName As String
            Public lastName As String
            Public Departments() As Department
        End Class
    
        Public Class Department
            Public departmentID As Integer
            Public Member As Boolean
        End Class
    
        Public Class Employee
            Public list() As list
        End Class
    
        Sub Main()
    
            Dim sResult As String = <_>
    {"list":[
    
    {"employeeId":1,"departmentId":4,"lastChanged":"2014-04-24 10:56:05","number":0,"uniqueId":"","firstName":"Jack","lastName":"Ryan",
    "departments":[{"departmentId":3,"member":true},{"departmentId":4,"member":false},{"departmentId":5,"member":true}]},
    
    {"employeeId":2,"departmentId":11,"lastChanged":"2014-04-23 11:17:01","number":12,"uniqueId":"","firstName":"Scott","lastName":"Smithers",
    "departments":[{"departmentId":3,"member":false},{"departmentId":4,"member":true},{"departmentId":5,"member":true}]},
    
    ]}
                                    </_>.Value
    
            Dim obj As Employee
            obj = JsonConvert.DeserializeObject(Of Employee)(sResult)
    
            Console.WriteLine("JSON: " + obj.list(0).firstName)
    
            Console.Read()
        End Sub
    I tested it and I can get: JSON: Jack

    • Proposed as answer by Carl CaiModerator Tuesday, May 6, 2014 3:36 AM
    • Marked as answer by Johngeh Tuesday, May 6, 2014 7:46 PM
    Tuesday, May 6, 2014 3:14 AM

All replies

  • Hi John,

    I don't afraid your code executes fine, you didn't catch the exception.

    Please check the whole code below: I changed the array definination to List, and I parse the data good.

    Imports Newtonsoft.Json
    
    Public Class Form1
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
            Dim sResult As String = FileIO.FileSystem.ReadAllText(".\data.txt")
            Dim obj As ListEmployee
            obj = JsonConvert.DeserializeObject(Of ListEmployee)(sResult)
    
            Console.WriteLine("JSON: " + obj.list(0).firstName)
    
        End Sub
    End Class
    
    Public Class ListEmployee
        Public list As List(Of Employee)
    End Class
    
    Public Class Employee
        Public employeeID As Integer
        Public departmentID As Integer
        Public lastChanged As String
        Public number As Integer
        Public displayName As String
        Public firstName As String
        Public lastName As String
        Public Property Departments As List(Of Department)
    End Class
    
    Public Class Department
        Public departmentID As Integer
        Public Member As Boolean
    End Class

    I hope this will be helpful.


    Mike Feng
    Help me, help you

    • Proposed as answer by Carl CaiModerator Tuesday, May 6, 2014 3:36 AM
    • Marked as answer by Johngeh Tuesday, May 6, 2014 7:46 PM
    Tuesday, May 6, 2014 3:12 AM
    Moderator
  • Hi, I think you need to change Public Property Departments() As Department to Public Departments() As Department.

    please see below code:

        Public Class list
            Public employeeID As Integer
            Public departmentID As Integer
            Public lastChanged As String
            Public number As Integer
            Public displayName As String
            Public firstName As String
            Public lastName As String
            Public Departments() As Department
        End Class
    
        Public Class Department
            Public departmentID As Integer
            Public Member As Boolean
        End Class
    
        Public Class Employee
            Public list() As list
        End Class
    
        Sub Main()
    
            Dim sResult As String = <_>
    {"list":[
    
    {"employeeId":1,"departmentId":4,"lastChanged":"2014-04-24 10:56:05","number":0,"uniqueId":"","firstName":"Jack","lastName":"Ryan",
    "departments":[{"departmentId":3,"member":true},{"departmentId":4,"member":false},{"departmentId":5,"member":true}]},
    
    {"employeeId":2,"departmentId":11,"lastChanged":"2014-04-23 11:17:01","number":12,"uniqueId":"","firstName":"Scott","lastName":"Smithers",
    "departments":[{"departmentId":3,"member":false},{"departmentId":4,"member":true},{"departmentId":5,"member":true}]},
    
    ]}
                                    </_>.Value
    
            Dim obj As Employee
            obj = JsonConvert.DeserializeObject(Of Employee)(sResult)
    
            Console.WriteLine("JSON: " + obj.list(0).firstName)
    
            Console.Read()
        End Sub
    I tested it and I can get: JSON: Jack

    • Proposed as answer by Carl CaiModerator Tuesday, May 6, 2014 3:36 AM
    • Marked as answer by Johngeh Tuesday, May 6, 2014 7:46 PM
    Tuesday, May 6, 2014 3:14 AM
  • Hi,

    Mike and Lapheal has pointed where the issue exists.

    The following tips are suggested just based on the JSON string.

    1. The Class Employee defined by yourself should contains a member named "list", and based on >>"list":[...<< and >>"departments":[...<<

    They contain "[", then their member should be an array or a list<of type>.

    Then you could try Mike's suggestion changing the Class name of that member and using 

    List(Of Employee) 

    and

    Public Property Departments As List(Of Department)

    Or you could code as Lapheal suggested

    list() As list

    and

       Public Departments() As Department

    If that should not be an array or List<of type>, then just remove "[" and "]" of your JSON string.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, May 6, 2014 3:36 AM
    Moderator
  • Thanks Mike and lapheal.  I implemented your suggestions and it is working.

    Here is what I came up with in the event that it can help someone else.

    
       Public Class Employees
            Public list As List(Of Employee)
        End Class
    
        Public Class Employee
            Public employeeID As Integer
            Public departmentID As Integer
            Public lastChanged As String
            Public displayName As String
            Public firstName As String
            Public lastName As String
            Public Departments As List(Of Department)
    
        End Class
    
        Public Class Department
            Public departmentID As Integer
            Public Member As Boolean
        End Class
    
    Public Sub ProcessJSON(ByVal sResult As String)
    
            Try
                Dim obj As JSON.Employees
                obj = JsonConvert.DeserializeObject(Of JSON.Employees)(sResult)
    
                For Each employee In obj.list
                    Debug.WriteLine("ID: " + CStr(employee.employeeID))
                    Debug.WriteLine("Department: " + CStr(employee.departmentID))
                    Debug.WriteLine("First Name: " + employee.firstName)
                    Debug.WriteLine("Last Name: " + employee.lastName)
                    Debug.WriteLine("Display Name: " + employee.displayName)
                    Debug.WriteLine("Last Changed: " + employee.lastChanged)
     
                    For Each department In employee.Departments
                        Debug.WriteLine("  Department: " + CStr(department.departmentID))
                        Debug.WriteLine("  Member: " + CStr(department.Member))
                         Debug.WriteLine("")
                    Next
                    Debug.WriteLine("")
                    Debug.WriteLine("")
                Next
    
            Catch ex As Exception
                Debug.WriteLine("JSON Error: " + ex.Message)
    
            End Try
    
        End Sub
    
    


    John

    Tuesday, May 6, 2014 7:50 PM