none
How to remove the json object by the key? RRS feed

  • Question

  • How to remove the json object (dateadded, guid, id, name, type, url) by the key (id). For example i need to remove the object (the Bold and italic part) by the id=10.

    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 code is below

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
            Dim listBookmarkBarChild As List(Of Child) = GetbookmarkbarBookmarkBarChildren(jsonTxt)
            Dim listOtherChild As List(Of Child) = GetbookmarkbarOtherChildren(jsonTxt)
            Dim merged As New List(Of Child)()
            merged.AddRange(listBookmarkBarChild)
            merged.AddRange(listOtherChild)
    
            For Each result As Child In merged
                Dim line As String = result._id & " " & result._dateadded & " " & result._guid & " " & result._name & " " & result._type & " " & result._url
                ListBox1.Items.Add(line)
            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
    
    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
            _guid = guid
            _id = id
            _name = name
            _type = type
            _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 10:33 AM

Answers

  • Hi,

    You can use JsonConvert.DeserializeObject

     Dim values = JsonConvert.DeserializeObject(Of Child)(jsonString)
            For Each item In values.Parameters
                
            Next
            JsonConvert.SerializeObject(values)

    see the following link:

    https://stackoverflow.com/questions/55969799/remove-json-object-in-json-array-in-c-sharp

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by gaxjyxq Tuesday, January 28, 2020 9:43 AM
    Tuesday, January 28, 2020 7:53 AM