Issue
i have some response form HTTP API - GeoJSON data, but how i can get only some values from features --> properties in Kotlin - Android Studio? Or is there some statement where I can determine for example all object where "year==2014" to array/list? So i want only some properties to array/list or variable. Thank you for your help :)
This working, but how to loop for each properties?
val test = jsonObject.getJSONArray("features").getJSONObject(0).getJSONObject("properties").getString("year")
{
"type": "FeatureCollection",
"name": "cycl_accidents_cycl_neh_xytabletopoint_spa1",
"crs": { "crs".
"type": "name",
"properties": { "properties".
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [
{
"type": "feature",
"properties": {
"objectid": 1,
"join_count": 1,
"target_fid": 1,
"id": 60040140073.0,
"d": -604774.832,
"e": -1162719.75,
"datum": "2014-08-03T00:00:00Z",
"day": 7,
"year": 2014,
"month": 8,
"collision": "other type of accident",
"cause": "driving on the wrong side of the road, driving into oncoming traffic",
"alcohol": "Yes, blood alcohol content between 0,8‰ and 1,0‰",
"at fault": "driver of a non-motor vehicle",
'consequences': 'accident resulting in death or injury',
'road condition': 'surface dry, not contaminated',
'weather conditions': 'not obstructed',
'visibility': 'at night - no public lighting, visibility not impaired by weather conditions',
'visibility': 'good',
'mishap': 'none or none of the above',
"type_of_community": "road monitored (in selected cities)",
'type of vehicle': 'bicycle',
'rider_status': 'under the influence of alcohol, blood alcohol content up to 0,99 ‰',
"affected_ride": 1,
"person": "driver",
"mark_person": "without helmet (only for motorcyclists or cyclists)",
"gender": "male",
"consequence": "slight injury",
"killed_os": 0,
"severely_injured_os": 0,
"slightly_injured_os": 1,
"material damage": 0,
"time": 430,
"hour": 4,
"month_t": "august",
"day of the week": "Sunday",
"age_group": "19-24",
"name": "Brno-Bosonohy",
"point_x": 16.52074206,
"point_y": 49.17113043000001,
"globalid": "{CD1B3254-7DD0-478E-8B3A-C6221ADC3DAB}"
},
"geometry": {
"type": "point",
"coordinates": [
16.520742066000025,
49.171130437000045
]
}
},
{
"type": "Feature",
"properties": {
"objectid": 2,
"join_count": 1,
"target_fid": 2,
"id": 60206100089.0,
"d": -596241.506,
"e": -1157759.125,
"datum": "2010-01-21T00:00:00Z",
"day": 4,
"year": 2010,
"month": 1,
"collision": "collision with a moving non-rail vehicle",
"cause": "driver not fully engaged in driving the vehicle",
"alcohol": "No",
"at fault": "by the driver of the motor vehicle",
'consequences': 'accident resulting in death or injury',
'road condition': 'surface dry, dirty (sand, dust, leaves, gravel, etc.)',
'weather conditions': 'not obstructed',
'visibility': 'at night - with public lighting, visibility not impaired by weather conditions',
'visibility': 'good',
'accident location': 'near a pedestrian crossing (within 20 m)',
'road type': 'local road',
'type of vehicle': 'bicycle',
'condition_ridic': 'good - no adverse circumstances detected',
"affected_ridice": 1,
"person": "driver",
"person_mark": "with helmet (only for motorcyclists or cyclists)",
"gender": "male",
"consequence": "slight injury",
"killed_os": 0,
"severely_injured_os": 0,
"slightly_injured_os": 1,
"material damage": 20,
"time": 1829,
"hour": 18,
"month_t": "January",
"day of the week": "Thursday",
"age_group": "33-44",
"name": "Brno-sever",
"point_x": 16.62980964,
"point_y": 49.22375737,
"globalid": "{83269995-7595-4174-B258-92B2231CF8F2}"
},
"geometry": {
"type": "point",
"coordinates": [
16.629809646000069,
49.223757371000033
]
}
},
]
}
Solution
I am assuming features is the list which you get after parsing your response.
first parse your response to your model class. please check this link for that. https://stackoverflow.com/a/68501606/7248394.
for example
val apiData = Gson().fromJson(readJson, YourModelClass::class.java)
val features = apiData.features
val featureList = features.filter{
it.properties.year =="2014"
}
so this will give featureList of 2014 year.
Answered By - Ankit
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.