Home > OS >  Extract data from a JSON received from an HTTP response
Extract data from a JSON received from an HTTP response

Time:04-30

So I have an HTTP response that gives me this data:

{
   "result":"success",
   "records":{
      "509442013":{
         "nif":509442013,
         "seo_url":"nexperience-lda",
         "title":"Nexperience Lda",
         "address":"Rua da Lionesa Nº 446, Edifício G20",
         "pc4":"4465",
         "pc3":"671",
         "city":"Leça do Balio",
         "activity":"Desenvolvimento de software. Consultoria em informática. Comércio de equipamentos e sistemas informáticos. Exploração de portais web.",
         "status":"active",
         "cae":"62010",
         "contacts":{
            "email":"[email protected]",
            "phone":"220198228",
            "website":"www.nex.pt",
            "fax":"224 905 459"
         },
         "structure":{
            "nature":"LDA",
            "capital":"5000.00",
            "capital_currency":"EUR"
         },
         "geo":{
            "region":"Porto",
            "county":"Matosinhos",
            "parish":"Leça do Balio"
         },
         "place":{
            "address":"Rua da Lionesa Nº 446, Edifício G20",
            "pc4":"4465",
            "pc3":"671",
            "city":"Leça do Balio"
         },
         "racius":"http://www.racius.com/nexperience-lda/",
         "alias":"Nex - Nexperience, Lda",
         "portugalio":"http://www.portugalio.com/nex/"
      }
   },
   "nif_validation":true,
   "is_nif":true,
   "credits":{
      "used":"free",
      "left":[
         
      ]
   }
}

And I need to extract some data from it, like the fields:
NIF
title
and the email and phone that are inside the contacts array.

I need the data to populate some TextBoxes in my Form.
To get the http response I have this button but I don't know how to extract the data I need.
Can someone help me?

My code:

Private Sub Btn_NIFImport_Click(sender As Object, e As EventArgs) Handles Btn_NIFImport.Click
    Dim key As String = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
    Try
        'Create the request
        Dim request As HttpWebRequest = HttpWebRequest.Create("http://www.nif.pt/?json=1&q=" & Txt_NIFImport.Text & "&key=" & key)
        request.Proxy = Nothing
        request.UserAgent = "Test"

        'Create the response reader
        Dim response As HttpWebResponse = request.GetResponse
        Dim responseStream As System.IO.Stream = response.GetResponseStream

        'Create a new stream reader
        Dim streamReader As New System.IO.StreamReader(responseStream)
        Dim data As String = streamReader.ReadToEnd
        streamReader.Close()

        'Display the data on the screen
        Txt_Teste.Text = data

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Thanks in advance.

CodePudding user response:

after you get the response from the http request you need to deserialize the json to an object in vb.net

To Classes to create the object by your json:

Public Class [Structure]
    Public Property nature As String
    Public Property capital As String
    Public Property capital_currency As String
End Class

Public Class Geo
    Public Property region As String
    Public Property county As String
    Public Property parish As String
End Class

Public Class Place
    Public Property address As String
    Public Property pc4 As String
    Public Property pc3 As String
    Public Property city As String
End Class

Public Class Record
    Public Property nif As Integer
    Public Property seo_url As String
    Public Property title As String
    Public Property address As String
    Public Property pc4 As String
    Public Property pc3 As String
    Public Property city As String
    Public Property activity As String
    Public Property status As String
    Public Property cae As String
    Public Property contacts As Contacts
    Public Property [structure] As [Structure]
    Public Property geo As Geo
    Public Property place As Place
    Public Property racius As String
    Public Property [alias] As String
    Public Property portugalio As String
End Class   

Public Class Credits
    Public Property used As String
    Public Property left As List(Of Object)
End Class

Public Class Root
    Public Property result As String
    Public Property records As Dictionary(Of String, Record)
    Public Property nif_validation As Boolean
    Public Property is_nif As Boolean
    Public Property credits As Credits
End Class

and to deserialize added after you get the response the following line:

 Root l = JsonConvert.DeserializeObject<Root>(response);

EDIT: I made some changes in the code so now you use with Dictionary that has KEY in your json '509442013' and value As Record (Notice that I change the class name as was before '_509442013') And in the root changes

 Public Property records As Dictionary(Of String, Record)

now this will work in each time even if you will get in each http request ID as class that not the same. notice that the only way you can get the value is by the KEY you already pass in the http request.

Added GIF: (In my case I change the ID and city, in the result show the city from the json)

enter image description here

Convert Button:

Dim jsonString As String = tbInput.Text
Dim l As Root = JsonConvert.DeserializeObject(Of Root)(jsonString)
tbResult.Text = l.records(tbKEY.Text.ToString).city.ToString()
  • tbKEY get the value from the screen like in my gif example.
  • Related