1

I am getting a JSON response when hitting an API using an HTTP Request/Response. This part of the code is working fine. I am struggling to convert the response to a usable format.

Here is the JSON response in string:

{
   "records":[
      {
         "id":"reciLUiZXJ8bs73JD",
         "fields":{
            "EstimatedDownTime":10,
            "MaintenanceRequired":true,
            "Reason":"Reason A",
            "Reporting":"Tim",
            "Notes":"Clean",
            "TimeStamp":"2020-06-29T13:16:24.000Z"
         },
         "createdTime":"2020-06-29T13:16:24.000Z"
      }
   ]
}

Current VB Code:

Dim json As String = myText
Dim jsonObject As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(json)
Dim jsonArray As JArray = jsonObject("fields")

For Each item As JObject In jsonArray
    MsgBox(item.SelectToken("Notes").ToString)
Next

Any help getting the fields into separate variables for use else where in the program would be great! Thanks

Update, Here is the VB code i am using to try to deserialize json. However I am not getting any results from the deserializer.

        myReq.Method = "GET"
    myReq.ContentType = "application/json"
    myReq.Headers.Add("Authorization: Bearer ************")
    myResp = myReq.GetResponse
    Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
    Dim myText As String
    myText = myreader.ReadToEnd

    MsgBox(myText)

    Dim account As Fields = JsonConvert.DeserializeObject(Of Fields)(myText)
    MsgBox(account.Notes)

Updated class list, Still not returning any values when calling:

Public Class Rootobject
    Public Property records() As Record
End Class

Public Class Record
    Public Property id As String
    Public Property fields() As Fields
    Public Property createdTime As DateTimeOffset
End Class

Public Class Fields
    Public Property EstimatedDownTime As Integer
    Public Property MaintenanceRequired As Boolean
    Public Property Reason As String
    Public Property Reporting As String
    Public Property Notes As String
    Public Property TimeStamp As Date
End Class
3
  • Look up the JSON Deserialize (/ deserializer) method - I used this quite a bit recently, where if you have a custom class that has properties and arrays that match the format of the JSON you're receiving, the JSONDeserializer can automatically MAP the json string into an instance of that custom class. It's magic! I believe you need the Netwonsoft.JSON nuget package for this! Commented Jun 29, 2020 at 17:42
  • How to create vb.net object class from json file (or xml file) Commented Jun 29, 2020 at 18:01
  • In your code, you're missing the records class object, which is an array of class objects. The fields property is also a class object, that you already have. Add the missing parts (createdTime is of type DateTimeOffset, some utilities may convert it as string or DateTime, which is not correct). Commented Jun 29, 2020 at 18:43

1 Answer 1

0

Here is a basic attempt:

Imports Newtonsoft.Json

Public Structure records
    Public records As List(Of record)
End Structure

Public Structure record
    Public id As String
    Public fields As Fields
    Public createdTime As DateTime
End Structure

Public Structure Fields
    Public EstimatedDownTime As Integer
    Public MaintenanceRequired As Boolean
    Public reason As String
    Public Reporting As String
    Public Notes As String
    Public TimeStamp As Date
End Structure

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim json As String = Me.TextBox1.Text
        Dim root As records = JsonConvert.DeserializeObject(Of records)(json)
        Dim r As record = root.records(0)

        Console.WriteLine("id: " & r.id)
        Console.WriteLine("createdTime: " & r.createdTime)

        ' fields
        Console.WriteLine("EstimatedDownTime: " & r.fields.EstimatedDownTime)
        Console.WriteLine("MaintenanceRequired: " & r.fields.MaintenanceRequired)
        Console.WriteLine("Reason: " & r.fields.reason)
        Console.WriteLine("Reporting: " & r.fields.Reporting)
        Console.WriteLine("Notes: " & r.fields.Notes)
        Console.WriteLine("TimeStamp: " & r.fields.TimeStamp)

    End Sub
End Class

Output:

id: reciLUiZXJ8bs73JD
EstimatedDownTime: 10
MaintenanceRequired: True
Reason: Reason A
Reporting: Tim
Notes: Clean
TimeStamp: 29/06/2020 13:16:24
createdTime: 29/06/2020 13:16:24

Note that I have simply used a structure (with a substructure) here instead of a class. As you like it.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.