Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
Dim strJSON As String = GenerateSampleJSON
Dim Data As List = ProcessJSON(strJSON)
' Now display the value
For i = 0 To Data.Size - 1
Dim Item As Map = Data.Get(i)
'Log( Item )
Log(" ")
Log(" ")
For Each key In Item.Keys
Log( Item.Get(key) )
Next
Next
End Sub
Private Sub ProcessJSON (JSON As String) As List
Dim parser As JSONParser
parser.Initialize(JSON)
Dim jRoot As Map = parser.NextObject
Dim Headline As Map = jRoot.Get("Headline")
'Dim EffectiveDate As String = Headline.Get("EffectiveDate")
'Dim EffectiveEpochDate As Int = Headline.Get("EffectiveEpochDate")
'Dim Severity As Int = Headline.Get("Severity")
Dim Text As String = Headline.Get("Text") 'Live status
Dim Category As String = Headline.Get("Category") 'image
'Dim EndEpochDate As Int = Headline.Get("EndEpochDate")'Not Used
'Dim EndDate As String = Headline.Get("EndDate") 'Not Used
Log(Text)
Log(Category)
Dim DailyForecasts As List = jRoot.Get("DailyForecasts")
Dim List1 As List
List1.Initialize
For Each colDailyForecasts As Map In DailyForecasts
Dim EpochDate As Int = colDailyForecasts.Get("EpochDate")
Dim Date As String = colDailyForecasts.Get("Date")
Dim Temperature As Map = colDailyForecasts.Get("Temperature")
Dim Maximum As Map = Temperature.Get("Maximum")
Dim Value As Double = Maximum.Get("Value")
Dim Day As Map = colDailyForecasts.Get("Day")
Dim Icon As Int = Day.Get("Icon")
Dim IconPhrase As String = Day.Get("IconPhrase") 'description
'Dim PrecipitationType As String = Day.Get("PrecipitationType")
'Dim HasPrecipitation As String = Day.Get("HasPrecipitation")
'Dim PrecipitationIntensity As String = Day.Get("PrecipitationIntensity")
'Dim Data() As Object
'Data = Array As String( _
'EpochDate,Date,Value,Icon,IconPhrase, _
'EpochDate,Date,Value,Icon,IconPhrase, _
'EpochDate,Date,Value,Icon,IconPhrase, _
'EpochDate,Date,Value,Icon,IconPhrase, _
'EpochDate,Date,Value,Icon,IconPhrase)
'Main.L.Initialize
'Main.L.AddAll(Data)
'For i = 0 To 4
' Main.L.Add(Data)
'Next
Dim Data As Map
Data.Initialize
Data.Put("EpochDate", EpochDate)
Data.Put("Date", Date)
Data.Put("Value", Value)
Data.Put("Icon", Icon)
Data.Put("IconPhrase", IconPhrase)
List1.Add(Data)
Next
Return List1
End Sub
Private Sub GenerateSampleJSON As String
Return $"{
"Headline": {
"EffectiveDate": "2022-08-15T08:00:00+02:00",
"EffectiveEpochDate": 1660543200,
"Severity": 5,
"Text": "A thunderstorm Monday",
"Category": "thunderstorm",
"EndDate": "2022-08-15T20:00:00+02:00",
"EndEpochDate": 1660586400,
"MobileLink": "http://www.accuweather.com/en/de/hemer/58675/daily-weather-forecast/174476?unit=c&lang=en-us",
"Link": "http://www.accuweather.com/en/de/hemer/58675/daily-weather-forecast/174476?unit=c&lang=en-us"
},
"DailyForecasts": [
{
"Date": "2022-08-11T07:00:00+02:00",
"EpochDate": 1660194000,
"Temperature": {
"Minimum": {
"Value": 16.1,
"Unit": "C",
"UnitType": 17
},
"Maximum": {
"Value": 28.1,
"Unit": "C",
"UnitType": 17
}
},
"Day": {
"Icon": 2,
"IconPhrase": "Mostly sunny",
"HasPrecipitation": false
},
"Night": {
"Icon": 33,
"IconPhrase": "Clear",
"HasPrecipitation": false
},
"Sources": [
"AccuWeather"
],
"MobileLink": "http://www.accuweather.com/en/de/hemer/58675/daily-weather-forecast/174476?day=1&unit=c&lang=en-us",
"Link": "http://www.accuweather.com/en/de/hemer/58675/daily-weather-forecast/174476?day=1&unit=c&lang=en-us"
},
{
"Date": "2022-08-12T07:00:00+02:00",
"EpochDate": 1660280400,
"Temperature": {
"Minimum": {
"Value": 17.5,
"Unit": "C",
"UnitType": 17
},
"Maximum": {
"Value": 28.3,
"Unit": "C",
"UnitType": 17
}
},
"Day": {
"Icon": 1,
"IconPhrase": "Sunny",
"HasPrecipitation": false
},
"Night": {
"Icon": 33,
"IconPhrase": "Clear",
"HasPrecipitation": false
},
"Sources": [
"AccuWeather"
],
"MobileLink": "http://www.accuweather.com/en/de/hemer/58675/daily-weather-forecast/174476?day=2&unit=c&lang=en-us",
"Link": "http://www.accuweather.com/en/de/hemer/58675/daily-weather-forecast/174476?day=2&unit=c&lang=en-us"
},
{
"Date": "2022-08-13T07:00:00+02:00",
"EpochDate": 1660366800,
"Temperature": {
"Minimum": {
"Value": 18.3,
"Unit": "C",
"UnitType": 17
},
"Maximum": {
"Value": 28.1,
"Unit": "C",
"UnitType": 17
}
},
"Day": {
"Icon": 2,
"IconPhrase": "Mostly sunny",
"HasPrecipitation": false
},
"Night": {
"Icon": 33,
"IconPhrase": "Clear",
"HasPrecipitation": false
},
"Sources": [
"AccuWeather"
],
"MobileLink": "http://www.accuweather.com/en/de/hemer/58675/daily-weather-forecast/174476?day=3&unit=c&lang=en-us",
"Link": "http://www.accuweather.com/en/de/hemer/58675/daily-weather-forecast/174476?day=3&unit=c&lang=en-us"
},
{
"Date": "2022-08-14T07:00:00+02:00",
"EpochDate": 1660453200,
"Temperature": {
"Minimum": {
"Value": 19.9,
"Unit": "C",
"UnitType": 17
},
"Maximum": {
"Value": 29.3,
"Unit": "C",
"UnitType": 17
}
},
"Day": {
"Icon": 3,
"IconPhrase": "Partly sunny",
"HasPrecipitation": false
},
"Night": {
"Icon": 36,
"IconPhrase": "Intermittent clouds",
"HasPrecipitation": false
},
"Sources": [
"AccuWeather"
],
"MobileLink": "http://www.accuweather.com/en/de/hemer/58675/daily-weather-forecast/174476?day=4&unit=c&lang=en-us",
"Link": "http://www.accuweather.com/en/de/hemer/58675/daily-weather-forecast/174476?day=4&unit=c&lang=en-us"
},
{
"Date": "2022-08-15T07:00:00+02:00",
"EpochDate": 1660539600,
"Temperature": {
"Minimum": {
"Value": 16.6,
"Unit": "C",
"UnitType": 17
},
"Maximum": {
"Value": 23.9,
"Unit": "C",
"UnitType": 17
}
},
"Day": {
"Icon": 4,
"IconPhrase": "Intermittent clouds",
"HasPrecipitation": true,
"PrecipitationType": "Rain",
"PrecipitationIntensity": "Moderate"
},
"Night": {
"Icon": 35,
"IconPhrase": "Partly cloudy",
"HasPrecipitation": false
},
"Sources": [
"AccuWeather"
],
"MobileLink": "http://www.accuweather.com/en/de/hemer/58675/daily-weather-forecast/174476?day=5&unit=c&lang=en-us",
"Link": "http://www.accuweather.com/en/de/hemer/58675/daily-weather-forecast/174476?day=5&unit=c&lang=en-us"
}
]
}"$
End Sub