Home > Mobile >  How to Parse json key and subkey in vb.net
How to Parse json key and subkey in vb.net

Time:12-26

My basic vb.net code is the following :

        Dim request As HttpWebRequest
        Dim response As HttpWebResponse = Nothing
        Dim reader As StreamReader

        request = DirectCast(WebRequest.Create("https://openiban.com/v2/calculate/DE/10011001/2624579856"), HttpWebRequest)

        response = DirectCast(request.GetResponse(), HttpWebResponse)
        reader = New StreamReader(response.GetResponseStream())

        Dim rawresp As String
        rawresp = reader.ReadToEnd()
        Dim jsonResulttodict = JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(rawresp)
        Dim ibanX = jsonResulttodict.Item("iban")
        Dim bicX = jsonResulttodict.Item("bic")
        txt1.Text = ibanX
        txt2.text = bicX

and the JSON is :

{
  "valid": true,
  "messages": [
    "Bank code valid: 10011001"
  ],
  "iban": "DE42100110012624579856",
  "bankData": {
    "bankCode": "10011001",
    "name": "N26 Bank",
    "zip": "10179",
    "city": "Berlin",
    "bic": "NTSBDEB1XXX"
  },
  "checkResults": {
    "bankCode": true
  }
}

so I'm receiving this error for the BIC: System.Collections.Generic.KeyNotFoundException: 'The given key was not present in the dictionary.'

question:

how to parse the subkey "bic"

thank you

tried to Put Dim bicX = jsonResulttodict.Item("bankData.bic")

CodePudding user response:

The bic element is not a Key of the Dictionary, it's a Property of the bankData object, so of course you're getting that exception.

You can parse the JSON with JsonObject.Parse(), then extract nested values using its indexing capabilities:
(Includes some refactoring of the original code; the HttpWebResponse and StreamReader need to be disposed)

Dim request = WebRequest.CreateHttp("https://openiban.com/v2/calculate/DE/10011001/2624579856")
Using response = DirectCast(request.GetResponse(), HttpWebResponse),
    reader = New StreamReader(response.GetResponseStream())
    Dim json = reader.ReadToEnd()
    Dim bankInfo As JObject = JObject.Parse(json)
    Dim iban = bankInfo("iban").ToString()
    Dim bic = bankInfo("bankData")("bic").ToString()
End Using

In case you're using .NET instead of .NET Framework, I suggest deserializing with System.Text.Json instead (since it's already there, though it can be used in .NET Framework, too, adding the NuGet Package).
Also, better move to HttpClient.
If your GUI Platform is WinForms (you have txt1.Text there), add async to the event handler (or any other method) that's running this code. E.g.:

Imports System.Net.Http
Imports System.Text.Json

Private Shared ReadOnly client As New HttpClient()
' [...]

Private Async Sub SomeButton_Click(sender As Object, e As EventArgs) Handles SomeButton.Click
    Dim json = Await client.GetStringAsync("https://openiban.com/v2/calculate/DE/10011001/2624579856")
    Dim bankInfo = JsonDocument.Parse(json)
    Dim iban = bankInfo.RootElement.GetProperty("iban").ToString()
    Dim bic = bankInfo.RootElement.GetProperty("bankData").GetProperty("bic").ToString()
End Sub
  • Related