从从 HTTP 响应接收到的 JSON 中提取数据

Extract data from a JSON received from an HTTP response

所以我有一个 HTTP 响应给我这个数据:

{
   "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":"info@nex.pt",
            "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":[
         
      ]
   }
}

我需要从中提取一些数据,比如字段:
NIF
title
emailphone 位于 contacts 数组中。

我需要数据来填充我的表单中的一些文本框。
要获得 http 响应,我有这个按钮,但我不知道如何提取我需要的数据。
有人可以帮助我吗?

我的代码:

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

提前致谢。

从 http 请求获得响应后,您需要将 json 反序列化为 vb.net

中的对象

到类由您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

并在收到以下行的响应后添加反序列化:

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

编辑: 我对代码进行了一些更改,所以现在您可以使用在您的 json '509442013' 中具有 KEY 且值为 As Record 的字典(请注意,我将 class 名称更改为之前的 '_509442013') 并在根更改

 Public Property records As Dictionary(Of String, Record)

现在,即使您在每个 http 请求 ID 中获得的 class 都不相同,这也会在每次都有效。 请注意,您可以获得该值的唯一方法是通过您已经在 http 请求中传递的 KEY。

已添加 GIF: (在我的例子中,我更改了 ID 和城市,结果显示来自 json 的城市)

转换按钮:

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 像我的 gif 示例一样从屏幕上获取值。