Android Question JSON Parsing issue - Bit of help required.

WebbyBoy

Member
Licensed User
Longtime User
Hi All,

I'm trying to parse something like the following JSON to extract specific values - such as description, name, temp etc. I've tried the examples & reworking them, but just cannot get it.


B4X:
{
     "coord": {
       "lon": -0.13,
       "lat": 51.51
     },
     "weather": [
       {
         "id": 300,
         "main": "Drizzle",
         "description": "light intensity drizzle",
         "icon": "09d"
       }
     ],
     "base": "stations",
     "main": {
       "temp": 280.32,
       "pressure": 1012,
       "humidity": 81,
       "temp_min": 279.15,
       "temp_max": 281.15
     },
     "visibility": 10000,
     "wind": {
       "speed": 4.1,
       "deg": 80
     },
     "clouds": {
       "all": 90
     },
     "dt": 1485789600,
     "sys": {
       "type": 1,
       "id": 5091,
       "message": 0.0103,
       "country": "GB",
       "sunrise": 1485762037,
       "sunset": 1485794875
     },
     "id": 2643743,
     "name": "London",
     "cod": 200
     }
 

Attachments

  • example.txt
    830 bytes · Views: 210

DonManfred

Expert
Licensed User
Longtime User
This site helps you getting the code to parse the json.

This is the code for your json
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim dt As Int = root.Get("dt")
Dim coord As Map = root.Get("coord")
Dim lon As Double = coord.Get("lon")
Dim lat As Double = coord.Get("lat")
Dim visibility As Int = root.Get("visibility")
Dim weather As List = root.Get("weather")
For Each colweather As Map In weather
Dim icon As String = colweather.Get("icon")
Dim description As String = colweather.Get("description")
Dim main As String = colweather.Get("main")
Dim id As Int = colweather.Get("id")
Next
Dim name As String = root.Get("name")
Dim cod As Int = root.Get("cod")
Dim main As Map = root.Get("main")
Dim temp As Double = main.Get("temp")
Dim temp_min As Double = main.Get("temp_min")
Dim humidity As Int = main.Get("humidity")
Dim pressure As Int = main.Get("pressure")
Dim temp_max As Double = main.Get("temp_max")
Dim clouds As Map = root.Get("clouds")
Dim all As Int = clouds.Get("all")
Dim id As Int = root.Get("id")
Dim sys As Map = root.Get("sys")
Dim country As String = sys.Get("country")
Dim sunrise As Int = sys.Get("sunrise")
Dim sunset As Int = sys.Get("sunset")
Dim id As Int = sys.Get("id")
Dim type As Int = sys.Get("type")
Dim message As Double = sys.Get("message")
Dim base As String = root.Get("base")
Dim wind As Map = root.Get("wind")
Dim deg As Int = wind.Get("deg")
Dim speed As Double = wind.Get("speed")
 
Last edited:
Upvote 0

WebbyBoy

Member
Licensed User
Longtime User
This site helps you getting the code to parse the json.

This is the code for your json
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim dt As Int = root.Get("dt")
Dim coord As Map = root.Get("coord")
Dim lon As Double = coord.Get("lon")
Dim lat As Double = coord.Get("lat")
Dim visibility As Int = root.Get("visibility")
Dim weather As List = root.Get("weather")
For Each colweather As Map In weather
Dim icon As String = colweather.Get("icon")
Dim description As String = colweather.Get("description")
Dim main As String = colweather.Get("main")
Dim id As Int = colweather.Get("id")
Next
Dim name As String = root.Get("name")
Dim cod As Int = root.Get("cod")
Dim main As Map = root.Get("main")
Dim temp As Double = main.Get("temp")
Dim temp_min As Double = main.Get("temp_min")
Dim humidity As Int = main.Get("humidity")
Dim pressure As Int = main.Get("pressure")
Dim temp_max As Double = main.Get("temp_max")
Dim clouds As Map = root.Get("clouds")
Dim all As Int = clouds.Get("all")
Dim id As Int = root.Get("id")
Dim sys As Map = root.Get("sys")
Dim country As String = sys.Get("country")
Dim sunrise As Int = sys.Get("sunrise")
Dim sunset As Int = sys.Get("sunset")
Dim id As Int = sys.Get("id")
Dim type As Int = sys.Get("type")
Dim message As Double = sys.Get("message")
Dim base As String = root.Get("base")
Dim wind As Map = root.Get("wind")
Dim deg As Int = wind.Get("deg")
Dim speed As Double = wind.Get("speed")

Sir, you are a legend. Thankyou for the help.
 
Upvote 0
Top