Greetings,
I'm learning how to work with JSON and did a lot of searching and came up with the following coding. It's just a few lines of code but I thought it is at least enough to pull some data out of the downloaded data. I'm sure I messed up with the coding in several places and would like your help in getting me back on track.
The data is coming from This API.
Here is the coding I have so far:
Processing never gets past the Job.Success line so I don't know why the job is not a success. That also means it never got to parse the JSON data.
I formatted the returned JSON data from the API in a formatter so it's easier to locate everything. The one field of data I'm interested in getting is under "title" which is nested under "now_playing". I'm not sure how to formulate my Get statement to grab that nested "title" value.
Here is the JSON from the API that I formatted:
I'm learning how to work with JSON and did a lot of searching and came up with the following coding. It's just a few lines of code but I thought it is at least enough to pull some data out of the downloaded data. I'm sure I messed up with the coding in several places and would like your help in getting me back on track.
The data is coming from This API.
Here is the coding I have so far:
B4X:
#Region Project Attributes
#ApplicationLabel: Test
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Private xui As XUI
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
Private Label1 As Label
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
Try
FetchJSON
Catch
Label1.Text = LastException
End Try
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub FetchJSON() As ResumableSub
Dim Job As HttpJob
Job.Initialize(Null, Me)
Job.Download("http://128.199.109.254/api/nowplaying")
Wait For (Job) JobDone(Job As HttpJob)
Label1.Text = "Job done success? " & Job.Success
If Job.Success Then
ToastMessageShow("Ready to parse.", True)
Wait For (ParseJSONResults(Job.GetString)) Complete (Completed As Boolean)
End If
Job.Release
Return True
End Sub
Sub ParseJSONResults(JSONResults As String) As ResumableSub
Dim Parser As JSONParser
Parser.Initialize(JSONResults)
Dim TheRoot As Map = Parser.NextObject
Label1.Text = "Text returned is: " & TheRoot.Get("title")
Return True
End Sub
Processing never gets past the Job.Success line so I don't know why the job is not a success. That also means it never got to parse the JSON data.
I formatted the returned JSON data from the API in a formatter so it's easier to locate everything. The one field of data I'm interested in getting is under "title" which is nested under "now_playing". I'm not sure how to formulate my Get statement to grab that nested "title" value.
Here is the JSON from the API that I formatted:
B4X:
[
{
"station": {
"id": 1,
"name": "VIFM",
"shortcode": "vifm",
"description": "Gaya Hidup Anda",
"frontend": "icecast",
"backend": "liquidsoap",
"listen_url": "http://128.199.109.254/radio/8000/128?1603494004",
"is_public": true,
"mounts": [
{
"path": "/320",
"is_default": false,
"id": 3,
"name": "MP3 320KBPS",
"url": "http://128.199.109.254/radio/8000/320?1603494004",
"bitrate": 320,
"format": "mp3",
"listeners": {
"current": 0,
"unique": 0,
"total": 0
}
},
{
"path": "/128",
"is_default": true,
"id": 4,
"name": "MP3 128KBPS",
"url": "http://128.199.109.254/radio/8000/128?1603494004",
"bitrate": 128,
"format": "mp3",
"listeners": {
"current": 0,
"unique": 0,
"total": 0
}
},
{
"path": "/64",
"is_default": false,
"id": 5,
"name": "AAC+ 64KBPS",
"url": "http://128.199.109.254/radio/8000/64?1603494004",
"bitrate": 64,
"format": "aac",
"listeners": {
"current": 1,
"unique": 1,
"total": 1
}
},
{
"path": "/32",
"is_default": false,
"id": 6,
"name": "AAC+ 32KBPS",
"url": "http://128.199.109.254/radio/8000/32?1603494004",
"bitrate": 32,
"format": "aac",
"listeners": {
"current": 0,
"unique": 0,
"total": 0
}
}
],
"remotes": []
},
"listeners": {
"current": 1,
"unique": 1,
"total": 1
},
"live": {
"is_live": false,
"streamer_name": "",
"broadcast_start": null
},
"now_playing": {
"elapsed": 80,
"remaining": 200,
"sh_id": 59999,
"played_at": 1603493930,
"duration": 280,
"playlist": "default",
"streamer": "",
"is_request": false,
"song": {
"id": "2b7b5ec098f4cd99fbf5af2d30a31675",
"text": "mawi - aduh saliha",
"artist": "mawi",
"title": "aduh saliha",
"album": "",
"lyrics": "",
"art": "http://128.199.109.254/static/img/generic_song.jpg",
"custom_fields": []
}
},
"playing_next": {
"sh_id": 60002,
"played_at": 0,
"duration": 264,
"playlist": "default",
"streamer": "",
"is_request": false,
"song": {
"id": "2261ceda7266de58fec0cc0bd714b336",
"text": "khalifah - mira",
"artist": "khalifah",
"title": "mira",
"album": "",
"lyrics": "",
"art": "http://128.199.109.254/api/station/1/art/9a2d36a597950e71433845c8-1603112710.jpg",
"custom_fields": []
}
},
"song_history": [
{
"sh_id": 59997,
"played_at": 1603493632,
"duration": 211,
"playlist": "default",
"streamer": "",
"is_request": false,
"song": {
"id": "70171ec85d4715d8179423e71a773869",
"text": "2NE1 - I Am the Best (내가 제일 잘 나가)",
"artist": "2NE1",
"title": "I Am the Best (내가 제일 잘 나가)",
"album": "2nd Mini Album",
"lyrics": "",
"art": "http://128.199.109.254/api/station/1/art/537d84f0d0a8c76bcd664f7f-1603086604.jpg",
"custom_fields": []
}
},
{
"sh_id": 59994,
"played_at": 1603493425,
"duration": 178,
"playlist": "default",
"streamer": "",
"is_request": false,
"song": {
"id": "aadf26ccdc8af61a58b18fddb52d8eb2",
"text": "aiman tino - ku hanya sayang padamu",
"artist": "aiman tino",
"title": "ku hanya sayang padamu",
"album": "",
"lyrics": "",
"art": "http://128.199.109.254/static/img/generic_song.jpg",
"custom_fields": []
}
},
{
"sh_id": 59992,
"played_at": 1603493092,
"duration": 309,
"playlist": "default",
"streamer": "",
"is_request": false,
"song": {
"id": "c76e4d85461c72238801cefc966851e0",
"text": "ska 86 ft. kalia siska - seberkas sinar",
"artist": "ska 86 ft. kalia siska",
"title": "seberkas sinar",
"album": "",
"lyrics": "",
"art": "http://128.199.109.254/static/img/generic_song.jpg",
"custom_fields": []
}
},
{
"sh_id": 59989,
"played_at": 1603492884,
"duration": 208,
"playlist": "default",
"streamer": "",
"is_request": false,
"song": {
"id": "a29458834aecd87363067dd47e881b85",
"text": "Billie Eilish - my future",
"artist": "Billie Eilish",
"title": "my future",
"album": "my future",
"lyrics": "",
"art": "http://128.199.109.254/api/station/1/art/053dade261aa206961e6bc9a-1603091711.jpg",
"custom_fields": []
}
},
{
"sh_id": 59987,
"played_at": 1603492657,
"duration": 222,
"playlist": "default",
"streamer": "",
"is_request": false,
"song": {
"id": "debd9f1d62379907956ac5d58dd80eae",
"text": "tegar - aku mahu pacaran",
"artist": "tegar",
"title": "aku mahu pacaran",
"album": "",
"lyrics": "",
"art": "http://128.199.109.254/static/img/generic_song.jpg",
"custom_fields": []
}
}
],
"cache": "station"
}
]