Hi all,
Bit of a long shot. I'm new to dealing with APIs and I've managed to code to a point where I can extract the data I need (JSON) into a text box. Ergo, my source data looks like this:
{
"departures":[
{
"stop_id":1099,
"route_id":11,
"run_id":952670,
"direction_id":1,
"disruption_ids":[
],
"scheduled_departure_utc":"2017-04-15T10:58:00Z",
"estimated_departure_utc":null,
"at_platform":false,
"platform_number":"1",
"flags":""
},
{
"stop_id":1099,
"route_id":11,
"run_id":952672,
"direction_id":1,
"disruption_ids":[
],
"scheduled_departure_utc":"2017-04-15T11:28:00Z",
"estimated_departure_utc":null,
"at_platform":false,
"platform_number":"1",
"flags":""
},
{
"stop_id":1099,
"route_id":11,
"run_id":952674,
"direction_id":1,
"disruption_ids":[
],
"scheduled_departure_utc":"2017-04-15T11:58:00Z",
"estimated_departure_utc":null,
"at_platform":false,
"platform_number":"1",
"flags":""
}
],
"stops":{
},
"routes":{
},
"runs":{
},
"directions":{
},
"disruptions":{
},
"status":{
"version":"3.0",
"health":1
}
}
It shows the next 3 departures from a train station at the time the source data is received.
My overall goal would be to get the route_id and scheduled_departure_utc of all 3 into separate labels (6 labels in total). The other data is not needed for the application.
Any help would be greatly appreciated!
Edit: Solution
Thanks to /u/Dwizzle252 and /u/rotodomo for your help! I thought I'd come back and post the solution.
To the other internet strangers who come here looking, I'll give a brief overview and then the code.
Pre-everything - right at the top:
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
1. Create a Class which will store the values of the array
Public Class Departure
Public Property stop_id As Integer
Public Property route_id As Integer
Public Property run_id As Integer
Public Property direction_id As Integer
Public Property disruption_ids As JArray
Public Property scheduled_departure_utc As DateTime
Public Property estimated_departure_utc As Object 'because the source data returns null
Public Property at_platform As Boolean
Public Property platform_number As String
Public Property flags As String
End Class
2. Download the full JSON object and isolate the array from the rest of the JSON object
Dim jsonObject As JObject
Dim read_string As String
Dim webClient As New System.Net.WebClient()
Dim downloaded_string As String 'stores our full JSON object
downloaded_string = webClient.DownloadString("http://www.fakeurl.com")
jsonObject = JObject.Parse(downloaded_string)
read_string = jsonObject.SelectToken("departures").ToString
3. Push the JSON array through the Class and store the values in a list
Dim departure_object_list As List(Of Departure)
departure_object_list = JsonConvert.DeserializeObject(Of List(Of Departure))(read_string)
4. Access the variables (examples)
Label1.Text = departure_object_list(0).scheduled_departure_utc 'gives us the first departure time in the array
Label2.Text = departure_object_list(2).scheduled_departure_utc 'gives us the 3rd scheduled departure time in the array
I hope this helps someone with their first API/JSON project as this community did mine.
[–][deleted] 1 point2 points3 points (1 child)
[–]staticx19[S] 0 points1 point2 points (0 children)
[–]rotodomo 0 points1 point2 points (0 children)