(NET) NET (2017)

How to parse JSON by Newtonsoft.Json (on example of FireFox Tab Session Manager and decrypted JwtSecurityToken)

I have some example of JSON parse in my site - Parse Yotube response by Newtonsoft.Json, and in this page want to add practice about JSON parsing. Below I show a some simple template to use Newtonsoft.Json, first part is the reading FireFox Tab Session Manager.



In top level this JSON you can see as this:



And code each internal part of this JSON (one information about one opened page will be look as this):



Finally code of reading this JSON, find information about opened page and save it to DB you can see below.


 411:  #Region "Import from TabSession manager"
 412:   
 413:      Private Sub ReadJsonFromFFTabSessionManager_Click(sender As Object, e As EventArgs) Handles ReadJsonFromFFTabSessionManager.Click
 414:          'https://github.com/sienori/Tab-Session-Manager/blob/master/src/
 415:          Dim X As New OpenFileDialog
 416:          X.Title = "Browse Json TabSessionManager backup file"
 417:          X.Filter = "JSON file (*.json)|*.json|All files (*.*)|*.*"
 418:          X.FilterIndex = 1
 419:          X.CheckFileExists = True
 420:          X.Multiselect = False
 421:          If DialogResult.OK = X.ShowDialog Then
 422:              Dim CurMax As Integer = MainForm.db1.Sites.Max(Function(Z) Z.Pos)
 423:              Dim JsonString As String = My.Computer.FileSystem.ReadAllText(X.FileName)
 424:              Dim Jarr As JArray = JArray.Parse(JsonString)
 425:              For Each One As JObject In Jarr
 426:                  Dim name As String = One("name")
 427:                  Dim windowsInfo As JObject = One("windowsInfo")
 428:                  Dim windowsNumber As Integer = One("windowsNumber")
 429:                  If windowsNumber = 1 Then
 430:                      Dim Key As Integer = windowsInfo.Values.Select(Function(Z) Z("id"))(0).Value(Of Integer)
 431:                      Dim tabsNumber As Integer = One("tabsNumber")
 432:                      Dim windows As JObject = One("windows")
 433:                      Dim TabList As JObject = windows(Key.ToString)
 434:                      For Each OneTabAsProp As JProperty In TabList.Children
 435:                          Dim OneTabAsObj As JObject = CType(OneTabAsProp.First, JObject)
 436:                          Dim TabUrl As String = OneTabAsObj("url").Value(Of String)
 437:                          Dim TabTitle As String = OneTabAsObj("title").Value(Of String)
 438:                          db1.Sites.Add((New Site With {
 439:                                       .URL = TabUrl,
 440:                                      .Login = "",
 441:                                      .Pass = "",
 442:                                      .Comment = TabTitle,
 443:                                      .Type = RowType.Count + 1,
 444:                                      .Pos = CurMax + 1}))
 445:                          CurMax += 1
 446:                      Next
 447:                      db1.SaveChanges()
 448:                      Dim TypeDictionary As String = My.Computer.FileSystem.ReadAllText(LoginForm.UrlTypeFileName, System.Text.Encoding.UTF8)
 449:                      TypeDictionary &= name & " (" & Now.ToShortDateString & ")" & vbCrLf
 450:                      My.Computer.FileSystem.WriteAllText(LoginForm.UrlTypeFileName, TypeDictionary.ToString, False)
 451:                      TypeComboBox_Refresh()
 452:                      AutoClosingMessageBox.Show("New type has adding" & vbCrLf & name)
 453:                  Else
 454:                      MsgBox("Import working only with one saved FF window, not for many")
 455:                  End If
 456:              Next
 457:          End If
 458:      End Sub
 459:   
 460:  #End Region



Next example is decrypt JwtSecurityToken, format it and show as good formated text.



In code below I decrypt this token by System.IdentityModel.Tokens.Jwt, format it and show in the form.


 155:      Function FormatLoginToken() As String
 156:          Dim X As New JwtSecurityTokenHandler
 157:          Dim AccessToken = X.ReadJwtToken(CurrentLoginToken.access_token)
 158:          Dim RefreshToken = X.ReadJwtToken(CurrentLoginToken.refresh_token)
 159:          Dim AccessTokenFormated = JsonConvert.SerializeObject(AccessToken.Payload, Formatting.Indented)
 160:          Dim RefreshTokenFormated = JsonConvert.SerializeObject(RefreshToken.Payload, Formatting.Indented)
 161:          Return "Expired:" & CurrentLoginToken.expire_token.ToString & vbCrLf & AccessTokenFormated & vbCrLf & RefreshTokenFormated
 162:      End Function




This is two links, how to implement the same authentication on the server side.



Comments ( )
Link to this page: //www.vb-net.com/NewtonJson/Index.htm
< THANKS ME>