none
Deserialize Chrome bookmarks using JSON using VB.NET RRS feed

  • Question

  • Hello,

    My Chrome bookmarks content is below

    {
       "checksum": "6bf34f912e074d6dc26a92ae6b9c7eff",
       "roots": {
          "bookmark_bar": {
             "children": [ {
                "date_added": "13224378541763350",
                "guid": "8711febe-419c-4da8-bfdf-e871a4185c0c",
                "id": "9",
                "name": "Yahoo",
                "type": "url",
                "url": "https://www.yahoo.com/"
             }, {
                "date_added": "13224378601163246",
                "guid": "92b9e1b8-935a-473e-8609-36f681fddd44",
                "id": "10",
                "name": "Msdn forums - Visual Basic",
                "type": "url",
                "url": "https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vbgeneral"
             } ],
             "date_added": "13209171729527352",
             "date_modified": "13224378887233972",
             "guid": "00000000-0000-4000-A000-000000000002",
             "id": "1",
             "name": "Bookmark Bar",
             "type": "folder"
          },
          "other": {
             "children": [ {
                "date_added": "13224378887233972",
                "guid": "46c75cfb-557b-4147-a7d2-8a46bfb038ae",
                "id": "11",
                "name": "Latest Updates - CodeProject",
                "type": "url",
                "url": "https://www.codeproject.com/script/articles/latest.aspx"
             }, {
                "date_added": "13224378948095465",
                "guid": "4eb3765b-2227-4d61-a860-c50432a73fc6",
                "id": "13",
                "name": "Windows Forum",
                "type": "url",
                "url": "https://windowsforum.com/"
             } ],
             "date_added": "13209171729527373",
             "date_modified": "13224378948095465",
             "guid": "00000000-0000-4000-A000-000000000003",
             "id": "5",
             "name": "Other",
             "type": "folder"
          },
          "synced": {
             "children": [  ],
             "date_added": "13209171729527374",
             "date_modified": "0",
             "guid": "00000000-0000-4000-A000-000000000004",
             "id": "7",
             "name": "Mobile device bookmarks",
             "type": "folder"
          }
       },
       "version": 1
    }

    The C# code can deserialize the json string above.

     List<Child> getbookmarkbar_children(string jsontxt)
            {
                dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(jsontxt);
                dynamic children = json.roots.bookmark_bar.children;

                List<Child> returnchildren = new List<Child>();
                foreach (Newtonsoft.Json.Linq.JObject child in children)
                {
                    returnchildren.Add(child.ToObject<Child>());
                }

                return returnchildren;
            }

    But I want to covert to VB.NET, for the keyword "dynamic" in C#, I add the "Option Strict Off", and use 'Object' to replace 'dynamic', but it will not work, can anyone deserialize json string using VB.NET?

      Private Function getbookmarkbar_children(ByVal jsontxt As String) As List(Of Child)
        
            Dim json As Object = Newtonsoft.Json.JsonConvert.DeserializeObject(jsontxt)
         
            Dim children As Object = json.roots.bookmark_bar.children

            Dim returnchildren As New List(Of Child)()
                For Each child As Newtonsoft.Json.Linq.JObject In children
                    returnchildren.Add(child.ToObject(Of Child)())
                Next child

                Return returnchildren
          End Function

    Friday, January 24, 2020 10:49 PM

Answers

  • I first took the C# code using dynamic

    public static List<Child> GetbookmarkbarChildren1(string sender)
    {
        dynamic json = JsonConvert.DeserializeObject(sender);
        dynamic children = json.roots.bookmark_bar.children;
        
        var returnChildren = new List<Child>();
    
        foreach (JObject child in children)
        {
            returnChildren.Add(child.ToObject<Child>());
        }
    
        return returnChildren;
    }

    To this

    public static List<Child> GetbookmarkbarChildren2(string sender)
    {
        JObject json = JObject.Parse(sender);
        JToken children = json["roots"]["other"]["children"];
    
        List<Child> returnChildren = new List<Child>();
    
        foreach (JObject child in children)
        {
            returnChildren.Add(child.ToObject<Child>());
        }
    
    
        return returnChildren;
    }

    So the only part I could not (at least with limited time) get and I know it can be done is the path I had to hard code but with a little tinkering that could be fixed too.

    So here is the complete VB.NET version where I read the json from a text file.

    Imports System.IO
    Imports Newtonsoft.Json.Linq
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim input As String = File.ReadAllText("bookmarks.json")
            Dim results = GetbookmarkbarBookmarkBarChildren(input)
    
            For Each result As Child In results
                Console.WriteLine($"{result.name} - {result.url}")
            Next
    
            Console.WriteLine()
    
            results = GetbookmarkbarOtherChildren(input)
    
            For Each result As Child In results
                Console.WriteLine($"{result.name} - {result.url}")
            Next
    
        End Sub
        Public Function GetbookmarkbarBookmarkBarChildren(sender As String) As List(Of Child)
    
            Dim json As JObject = JObject.Parse(sender)
            Dim children As JToken = json("roots")("bookmark_bar")("children")
    
            Dim returnChildren As New List(Of Child)()
    
            For Each child As JObject In children
                returnChildren.Add(child.ToObject(Of Child)())
            Next
    
            Return returnChildren
    
        End Function
        Public Function GetbookmarkbarOtherChildren(sender As String) As List(Of Child)
    
            Dim json As JObject = JObject.Parse(sender)
            Dim children As JToken = json("roots")("other")("children")
    
            Dim returnChildren As New List(Of Child)()
    
            For Each child As JObject In children
                returnChildren.Add(child.ToObject(Of Child)())
            Next
    
            Return returnChildren
    
        End Function
    End Class
    Public Class Child
        Public Sub New(
           date_added As String,
           guid As String,
           id As String,
           name As String,
           type As String,
           url As String)
    
            dateadded = date_added
            Me.guid = guid
            Me.id = id
            Me.name = name
            Me.type = type
            Me.url = url
        End Sub
    
        Public Property dateadded() As String
        Public Property guid() As String
        Public Property id() As String
        Public Property name() As String
        Public Property type() As String
        Public Property url() As String
    End Class
    

    Output

    Yahoo - https://www.yahoo.com/
    Msdn forums - Visual Basic - https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vbgeneral
    
    Latest Updates - CodeProject - https://www.codeproject.com/script/articles/latest.aspx
    Windows Forum - https://windowsforum.com/
    

    Now the challenges at this point

    • How to move the code into one function.
    • How to iterate nodes in one function.

    I don't have any more time to put into this so hopefully this is good enough.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by gaxjyxq Saturday, January 25, 2020 3:28 AM
    Saturday, January 25, 2020 2:44 AM
    Moderator

All replies

  • Hello,

    Can you provide the Child class ?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, January 25, 2020 12:01 AM
    Moderator
  • Okay, I need to be away from the computer.

    Try the following. I placed your json into a text file named bookmarks.json and used the following code which of course still needs a class definition.

    Imports System.IO
    Imports Newtonsoft.Json
    Imports Newtonsoft.Json.Linq
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim json As String = File.ReadAllText("bookmarks.json")
            Using reader As New StringReader(json)
                Using jsonReader As JsonReader = New JsonTextReader(reader)
                    Dim serializer As New JsonSerializer()
                    Dim deserializer = CType(serializer.Deserialize(jsonReader), JToken)
                End Using
            End Using
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, January 25, 2020 12:17 AM
    Moderator
  • Public Class Child
            Public Sub New(ByVal date_added As String, ByVal guid As String, ByVal id As String, ByVal name As String, ByVal type As String, ByVal url As String)
                Me.dateadded = date_added
                Me.guid = guid
                Me.id = id
                Me.name = name
                Me.type = type
                Me.url = url
            End Sub

            Public Property dateadded() As String
            Public Property guid() As String
            Public Property id() As String
            Public Property name() As String
            Public Property type() As String
            Public Property url() As String
        End Class

    Saturday, January 25, 2020 1:32 AM
  • But how to get the child class list for "json.roots.bookmark_bar.children"?
    Saturday, January 25, 2020 1:33 AM
  • I first took the C# code using dynamic

    public static List<Child> GetbookmarkbarChildren1(string sender)
    {
        dynamic json = JsonConvert.DeserializeObject(sender);
        dynamic children = json.roots.bookmark_bar.children;
        
        var returnChildren = new List<Child>();
    
        foreach (JObject child in children)
        {
            returnChildren.Add(child.ToObject<Child>());
        }
    
        return returnChildren;
    }

    To this

    public static List<Child> GetbookmarkbarChildren2(string sender)
    {
        JObject json = JObject.Parse(sender);
        JToken children = json["roots"]["other"]["children"];
    
        List<Child> returnChildren = new List<Child>();
    
        foreach (JObject child in children)
        {
            returnChildren.Add(child.ToObject<Child>());
        }
    
    
        return returnChildren;
    }

    So the only part I could not (at least with limited time) get and I know it can be done is the path I had to hard code but with a little tinkering that could be fixed too.

    So here is the complete VB.NET version where I read the json from a text file.

    Imports System.IO
    Imports Newtonsoft.Json.Linq
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim input As String = File.ReadAllText("bookmarks.json")
            Dim results = GetbookmarkbarBookmarkBarChildren(input)
    
            For Each result As Child In results
                Console.WriteLine($"{result.name} - {result.url}")
            Next
    
            Console.WriteLine()
    
            results = GetbookmarkbarOtherChildren(input)
    
            For Each result As Child In results
                Console.WriteLine($"{result.name} - {result.url}")
            Next
    
        End Sub
        Public Function GetbookmarkbarBookmarkBarChildren(sender As String) As List(Of Child)
    
            Dim json As JObject = JObject.Parse(sender)
            Dim children As JToken = json("roots")("bookmark_bar")("children")
    
            Dim returnChildren As New List(Of Child)()
    
            For Each child As JObject In children
                returnChildren.Add(child.ToObject(Of Child)())
            Next
    
            Return returnChildren
    
        End Function
        Public Function GetbookmarkbarOtherChildren(sender As String) As List(Of Child)
    
            Dim json As JObject = JObject.Parse(sender)
            Dim children As JToken = json("roots")("other")("children")
    
            Dim returnChildren As New List(Of Child)()
    
            For Each child As JObject In children
                returnChildren.Add(child.ToObject(Of Child)())
            Next
    
            Return returnChildren
    
        End Function
    End Class
    Public Class Child
        Public Sub New(
           date_added As String,
           guid As String,
           id As String,
           name As String,
           type As String,
           url As String)
    
            dateadded = date_added
            Me.guid = guid
            Me.id = id
            Me.name = name
            Me.type = type
            Me.url = url
        End Sub
    
        Public Property dateadded() As String
        Public Property guid() As String
        Public Property id() As String
        Public Property name() As String
        Public Property type() As String
        Public Property url() As String
    End Class
    

    Output

    Yahoo - https://www.yahoo.com/
    Msdn forums - Visual Basic - https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=vbgeneral
    
    Latest Updates - CodeProject - https://www.codeproject.com/script/articles/latest.aspx
    Windows Forum - https://windowsforum.com/
    

    Now the challenges at this point

    • How to move the code into one function.
    • How to iterate nodes in one function.

    I don't have any more time to put into this so hopefully this is good enough.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by gaxjyxq Saturday, January 25, 2020 3:28 AM
    Saturday, January 25, 2020 2:44 AM
    Moderator
  • Thank you, that works well.

    How to remove a group data by id? for example i want to remove the node data (include the 'dateadded', 'guid', 'name'...) as id=1.

    • Edited by gaxjyxq Saturday, January 25, 2020 4:32 AM
    Saturday, January 25, 2020 3:29 AM
  • Thank you, that works well.

    How to remove a group data by id? for example i want to remove the node data (include the 'dateadded', 'guid', 'name'...) as id=1.

    Hello,

    I don't know and with that would recommend you start a new question which you can relate back to this question for reference.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, January 25, 2020 10:09 AM
    Moderator