How can I iterate over each item and render on the screen? - json
I've been trying to iterate over each item for forecast data and can't figure out where the outputs are coming from.. I'm only trying to iterate over temperature, pressure, place name, humidity, wind speed and feels like..
Json
{
"cod": "200",
"message": 0,
"cnt": 40,
"list": [
{
"dt": 1641124800,
"main": {
"temp": 11.74,
"feels_like": 11.08,
"temp_min": 11.74,
"temp_max": 12,
"pressure": 1012,
"sea_level": 1012,
"grnd_level": 1008,
"humidity": 81,
"temp_kf": -0.26
},
"weather": [
{
"id": 803,
"main": "Clouds",
"description": "broken clouds",
"icon": "04d"
}
],
"clouds": {
"all": 75
},
"wind": {
"speed": 5.3,
"deg": 229,
"gust": 11.75
},
"visibility": 10000,
"pop": 0,
"sys": {
"pod": "d"
},
"dt_txt": "2022-01-02 12:00:00"
},
{
"dt": 1641135600,
"main": {
"temp": 11.86,
"feels_like": 11.11,
"temp_min": 11.86,
"temp_max": 12.09,
"pressure": 1011,
"sea_level": 1011,
"grnd_level": 1006,
"humidity": 77,
"temp_kf": -0.23
},
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
],
"clouds": {
"all": 83
},
"wind": {
"speed": 6.45,
"deg": 223,
"gust": 15.9
},
"visibility": 10000,
"pop": 0.7,
"rain": {
"3h": 0.6
},
"sys": {
"pod": "d"
},
"dt_txt": "2022-01-02 15:00:00"
},
{
"dt": 1641146400,
"main": {
"temp": 11.29,
"feels_like": 10.43,
"temp_min": 11.06,
"temp_max": 11.29,
"pressure": 1011,
"sea_level": 1011,
"grnd_level": 1007,
"humidity": 75,
"temp_kf": 0.23
},
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10n"
}
],
"clouds": {
"all": 92
},
"wind": {
"speed": 6.1,
"deg": 250,
"gust": 13.97
},
"visibility": 10000,
"pop": 0.94,
"rain": {
"3h": 1.01
},
"sys": {
"pod": "n"
},
"dt_txt": "2022-01-02 18:00:00"
},
{
"dt": 1641157200,
"main": {
"temp": 9.54,
"feels_like": 6.37,
"temp_min": 9.54,
"temp_max": 9.54,
"pressure": 1012,
"sea_level": 1012,
"grnd_level": 1009,
"humidity": 76,
"temp_kf": 0
},
"weather": [
{
"id": 802,
"main": "Clouds",
"description": "scattered clouds",
"icon": "03n"
}
],
"clouds": {
"all": 36
},
"wind": {
"speed": 6.93,
"deg": 244,
"gust": 14.72
},
"visibility": 10000,
"pop": 0,
"sys": {
"pod": "n"
},
"dt_txt": "2022-01-02 21:00:00"
},
{
"dt": 1641546000,
"main": {
"temp": 3.96,
"feels_like": 0.41,
"temp_min": 3.96,
"temp_max": 3.96,
"pressure": 1010,
"sea_level": 1010,
"grnd_level": 1007,
"humidity": 82,
"temp_kf": 0
},
"weather": [
{
"id": 804,
"main": "Clouds",
"description": "overcast clouds",
"icon": "04d"
}
],
"clouds": {
"all": 100
},
"wind": {
"speed": 4.29,
"deg": 239,
"gust": 11.29
},
"visibility": 10000,
"pop": 0.02,
"sys": {
"pod": "d"
},
"dt_txt": "2022-01-07 09:00:00"
}
],
"city": {
"id": 2643743,
"name": "London",
"coord": {
"lat": 51.5085,
"lon": -0.1257
},
"country": "GB",
"population": 1000000,
"timezone": 0,
"sunrise": 1641110761,
"sunset": 1641139355
}
}
in forecast.dart
import 'package:forecast/models/weather_data.dart';
class ForecastData {
final List list;
ForecastData({required this.list});
factory ForecastData.fromJson(Map<String, dynamic> json) {
var list = json['list']?.map((e) => e)?.toList(growable: true) ?? [];
List weatherData = [];
list.forEach((e) {
WeatherData w = WeatherData(
placeName: json['city']['name'],
temperature: e['main']['temp'],
feels_like: e['main']["feels_like"],
pressure: e['main']['pressure'],
humidity: e['main']['humidity'],
wind_speed: e['wind']['speed']);
weatherData.add(w);
print(weatherData.toList());
});
return ForecastData(list: weatherData);
}
}
weatherdata.dart
class WeatherData {
String? placeName;
num? temperature;
num? feels_like;
num? pressure;
num? humidity;
num? wind_speed;
WeatherData({
this.placeName,
this.temperature,
this.feels_like,
this.pressure,
this.humidity,
this.wind_speed,
});
#override
String toString() =>
'${placeName ?? 'unknown'}${feels_like ?? 'unknown'}${temperature ?? 'unknown'}${pressure ?? 'unknown'}${humidity ?? 'unknown'}${wind_speed ?? 'unknown'}';
WeatherData.fromJson(Map<String, dynamic> json) {
placeName = json['city']['name'];
temperature = json['list'][0]['main']['temp'];
feels_like = json['list'][0]['main']['temp'];
pressure = json['list'][0]['main']['temp'];
humidity = json['list'][0]['main']['temp'];
wind_speed = json['list'][0]['main']['temp'];
}
}
output
I/flutter (25471): [London11.0811.741012815.3]
I/flutter (25471): [London11.0811.741012815.3, London11.1111.861011776.45]
I/flutter (25471): [London11.0811.741012815.3, London11.1111.861011776.45, London10.4311.291011756.1]
I/flutter (25471): [London11.0811.741012815.3, London11.1111.861011776.45, London10.4311.291011756.1, London6.379.541012766.93]
I/flutter (25471): [London11.0811.741012815.3, London11.1111.861011776.45, London10.4311.291011756.1, London6.379.541012766.93, London6.229.481013757.18]
I/flutter (25471): [London11.0811.741012815.3, London11.1111.861011776.45, London10.4311.291011756.1, London6.379.541012766.93, London6.229.481013757.18, London6.039.051013796.02]
I/flutter (25471): [London11.0811.741012815.3, London11.1111.861011776.45, London10.4311.291011756.1, London6.379.541012766.93, London6.229.481013757.18, London6.039.051013796.02, London5.698.681012795.66]
I can't figure out where the problem is..why is my output iterating like this? I just need to iterate over each item and print out each item each time please help!
in forecast.dart, You are printing the whole weatherData list in the iterator list.forEach.
Try changing ,
print(weatherData.toList());
to,
print(w);
And put some spaces in your toString
${placeName ?? 'unknown'} ${feels_like ?? 'unknown'} ${te...
so that you can see where one number ends and the next starts.
I think that you shoul lose the map and review the fromJson parameters...
WeatherData.fromJson(Map<String, dynamic> json) {
placeName = json['city']['name'];
temperature = json['list'][0]['main']['temp'];
feels_like = json['list'][0]['main']['temp'];
pressure = json['list'][0]['main']['temp'];
humidity = json['list'][0]['main']['temp'];
wind_speed = json['list'][0]['main']['temp'];
}
Related
Problem getting data from json through http with riverpod
I have this json: { "coord": { "lon": -3.7026, "lat": 40.4165 }, "weather": [ { "id": 800, "main": "Clear", "description": "clear sky", "icon": "01n" } ], "base": "stations", "main": { "temp": 297.84, "feels_like": 297.62, "temp_min": 294.82, "temp_max": 299.94, "pressure": 1017, "humidity": 48 }, "visibility": 10000, "wind": { "speed": 1.34, "deg": 270, "gust": 2.68 }, "clouds": { "all": 0 }, "dt": 1631051279, "sys": { "type": 2, "id": 2007545, "country": "ES", "sunrise": 1630993686, "sunset": 1631039875 }, "timezone": 7200, "id": 3117735, "name": "Madrid", "cod": 200 } with this link: http://api.openweathermap.org/data/2.5/weather?id=3117735&appid=899fc742471c1a7623a573d0bc7ad85b I want to get the data, to make that I created the class with a json convert, and get want to get the data through a json.decode: Future<Madrid> fetchMadrid() async { final response = await http .get(Uri.parse('http://api.openweathermap.org/data/2.5/weather?id=3117735&appid=899fc742471c1a7623a573d0bc7ad85b')) .timeout(const Duration(seconds: 2)); if (response.statusCode == 200) { return parseMadrid(response.body); } else { throw Exception('Error'); } } Madrid parseMadrid(String response) { var list = json.decode(response); Madrid photos = Madrid.fromJson(list); return photos; } final madridStateProvider = FutureProvider<Madrid>((ref) async { return fetchMadridLocal(); }); and get the data through a Future Provider with riverbed. I get the error: Expected a value of type 'Coord', but got one of type '_JsonMap'
ERROR: type 'String' is not a subtype of type 'int' of 'index' while using fromjson in flutter
I am learning TDD(Test Drive Development) in a flutter. Here I am trying to parse my JSON data but every time I run the test I get an error saying this. ERROR: type 'String' is not a subtype of type 'int' of 'index' These are my code weather_app_model_test.dart group('fromJson', () { test('should return a valid model', () async { final Map<String, dynamic> jsonMap = json.decode(fixture('weather_app.json')); final result = WeatherAppModel.fromJson(jsonMap); expect(result, tWeatherAppModel); }); }); weather_app_model.dart factory WeatherAppModel.fromJson(Map<String, dynamic> json) { return WeatherAppModel( weatherMain: json['weather']['main'], weatherDescription: json['weather']['description'], temp: json['main']['temp'], minTemp: json['main']['temp_min'], maxTemp: json['main']['temp_main'], country: json['sys']['country'], ); } fixtures/weather_app.json { "coord": { "lon": 78, "lat": 20 }, "weather": [ { "id": 500, "main": "Rain", "description": "light rain", "icon": "10d" } ], "base": "model", "main": { "temp": 301.51, "pressure": 1014, "humidity": 67, "temp_min": 301.51, "temp_max": 301.51, "sea_level": 1014, "grnd_level": 979 }, "wind": { "speed": 3.19, "deg": 77 }, "rain": { "3h": 1.81 }, "clouds": { "all": 45 }, "dt": 1572672029, "sys": { "country": "IN", "sunrise": 1572655775, "sunset": 1572696807 }, "timezone": 19800, "id": 1272596, "name": "Digras", "cod": 200 } Any help would be great.
factory WeatherAppModel.fromJson(Map<String, dynamic> json) { return WeatherAppModel( weatherMain: json['weather'][0]['main'], weatherDescription: json['weather'][0]['description'], temp: json['main']['temp'], minTemp: json['main']['temp_min'], maxTemp: json['main']['temp_main'], country: json['sys']['country'], ); } This will fix your problem. json['weather'] is not returning like map, its an array. It takes first value with [0].
How do I access the name of the city nested in this api response?
I request data with axios.get(...), successful get the return as nested objects. I can access the data.message, data.cnt, data.cod but I can't access the properties of the "city". kept getting undefine or typeerror. I want to access the properties of the city object nested within the response data. like this "data.city.name" but errors. { "cod": "200", "message": 0.0051, "cnt": 40, "list": [ { "dt": 1545318000, "main": { "temp": 282.74, "temp_min": 282.167, "temp_max": 282.74, "pressure": 1012.86, "sea_level": 1020.54, "grnd_level": 1012.86, "humidity": 84, "temp_kf": 0.57 }, "weather": [ { "id": 500, "main": "Rain", "description": "light rain", "icon": "10d" } ], "clouds": { "all": 44 }, "wind": { "speed": 5.67, "deg": 243.503 }, "rain": { "3h": 0.25 }, "sys": { "pod": "d" }, "dt_txt": "2018-12-20 15:00:00" }, { "dt": 1545739200, "main": { "temp": 282.628, "temp_min": 282.628, "temp_max": 282.628, "pressure": 1037.58, "sea_level": 1045.29, "grnd_level": 1037.58, "humidity": 100, "temp_kf": 0 }, "weather": [ { "id": 500, "main": "Rain", "description": "light rain", "icon": "10d" } ], "clouds": { "all": 76 }, "wind": { "speed": 2.02, "deg": 212.503 }, "rain": { "3h": 0.13 }, "sys": { "pod": "d" }, "dt_txt": "2018-12-25 12:00:00" } ], "city": { "id": 2643743, "name": "London", "coord": { "lat": 51.5073, "lon": -0.1277 }, "country": "GB", "population": 1000000 } }
Use JSON.parse(); Example: var data = JSON.parse(yourJSONObject) console.log(data.city.name)
May not full represent what you need, but this should show how to set a default state, fetch your results, and then pass them into components. Your object is fine, I feel like you might be trying to access it before Axios completes. export class DataView extends Component { constructor(props) { super(props); this.state = { isLoading: true, data: {} }; this.defaultState = { // default properties of state }; } componentWillMount() { this.fetchData(); } fetchData = () => { await axios.get( ${apiEndpoint}${inputCity}&appid=${apiKey} ) .then( data => { this.setState({ data, ...this.defaultState, isLoading: false }); }); }; render() { return ( <Fragment> {this.state.isLoading ? ( // Check if it's still loading and load nothing if true <Fragment /> ) : ( <Component withProps={this.state.data}></Component> // Data can be accessed if isLoading: false )} </Fragment> ); } }
if you are getting data.message, then you must get the city name with data.city.name
I think you have some errors in your code because i can access the data and the json is also valid too. try to console.log() the response and see what's going on.
Seems to work fine with data.city.name const data = { "cod": "200", "message": 0.0051, "cnt": 40, "list": [ { "dt": 1545318000, "main": { "temp": 282.74, "temp_min": 282.167, "temp_max": 282.74, "pressure": 1012.86, "sea_level": 1020.54, "grnd_level": 1012.86, "humidity": 84, "temp_kf": 0.57 }, "weather": [ { "id": 500, "main": "Rain", "description": "light rain", "icon": "10d" } ], "clouds": { "all": 44 }, "wind": { "speed": 5.67, "deg": 243.503 }, "rain": { "3h": 0.25 }, "sys": { "pod": "d" }, "dt_txt": "2018-12-20 15:00:00" }, { "dt": 1545739200, "main": { "temp": 282.628, "temp_min": 282.628, "temp_max": 282.628, "pressure": 1037.58, "sea_level": 1045.29, "grnd_level": 1037.58, "humidity": 100, "temp_kf": 0 }, "weather": [ { "id": 500, "main": "Rain", "description": "light rain", "icon": "10d" } ], "clouds": { "all": 76 }, "wind": { "speed": 2.02, "deg": 212.503 }, "rain": { "3h": 0.13 }, "sys": { "pod": "d" }, "dt_txt": "2018-12-25 12:00:00" } ], "city": { "id": 2643743, "name": "London", "coord": { "lat": 51.5073, "lon": -0.1277 }, "country": "GB", "population": 1000000 } } document.write(data.city.name); getData = async () => { const { inputCity } = this.state; try { const data = await axios.get(${apiEndpoint}${inputCity}&appid=${apiKey}); this.setState({ data }); console.log(this.state.data); return data; } catch (err) { console.error(err); } }
How to extract list [] from Json file with AESON
I have this JSON code { "cod": "200", "message": 0.0205, "cnt": 40, "list": [{ "dt": 1529690400, "main": { "temp": 294.89, "temp_min": 289.889, "temp_max": 294.89, "pressure": 875.6, "sea_level": 1022.36, "grnd_level": 875.6, "humidity": 73, "temp_kf": 5 }, "weather": [{ "id": 500, "main": "Rain", "description": "light rain", "icon": "10n" }], "clouds": { "all": 32 }, "wind": { "speed": 0.76, "deg": 355 }, "rain": { "3h": 0.0625 }, "sys": { "pod": "n" }, "dt_txt": "2018-06-22 18:00:00" }, { "dt": 1529701200, "main": { "temp": 291.2, "temp_min": 287.449, "temp_max": 291.2, "pressure": 875.14, "sea_level": 1022.14, "grnd_level": 875.14, "humidity": 82, "temp_kf": 3.75 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "02n" }], "clouds": { "all": 8 }, "wind": { "speed": 0.76, "deg": 7.00244 }, "rain": {}, "sys": { "pod": "n" }, "dt_txt": "2018-06-22 21:00:00" }, { "dt": 1529712000, "main": { "temp": 288.19, "temp_min": 285.69, "temp_max": 288.19, "pressure": 874.94, "sea_level": 1022.29, "grnd_level": 874.94, "humidity": 88, "temp_kf": 2.5 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01n" }], "clouds": { "all": 0 }, "wind": { "speed": 0.26, "deg": 237 }, "rain": {}, "sys": { "pod": "n" }, "dt_txt": "2018-06-23 00:00:00" }, { "dt": 1529722800, "main": { "temp": 288.65, "temp_min": 287.398, "temp_max": 288.65, "pressure": 875.18, "sea_level": 1022.74, "grnd_level": 875.18, "humidity": 86, "temp_kf": 1.25 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" }], "clouds": { "all": 0 }, "wind": { "speed": 0.37, "deg": 91.5048 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-23 03:00:00" }, { "dt": 1529733600, "main": { "temp": 294.069, "temp_min": 294.069, "temp_max": 294.069, "pressure": 874.97, "sea_level": 1021.62, "grnd_level": 874.97, "humidity": 73, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" }], "clouds": { "all": 0 }, "wind": { "speed": 1.6, "deg": 237.003 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-23 06:00:00" }, { "dt": 1529744400, "main": { "temp": 297.09, "temp_min": 297.09, "temp_max": 297.09, "pressure": 874.08, "sea_level": 1020, "grnd_level": 874.08, "humidity": 70, "temp_kf": 0 }, "weather": [{ "id": 500, "main": "Rain", "description": "light rain", "icon": "10d" }], "clouds": { "all": 0 }, "wind": { "speed": 0.85, "deg": 306.003 }, "rain": { "3h": 0.255 }, "sys": { "pod": "d" }, "dt_txt": "2018-06-23 09:00:00" }, { "dt": 1529755200, "main": { "temp": 298.961, "temp_min": 298.961, "temp_max": 298.961, "pressure": 873.46, "sea_level": 1019.23, "grnd_level": 873.46, "humidity": 53, "temp_kf": 0 }, "weather": [{ "id": 500, "main": "Rain", "description": "light rain", "icon": "10d" }], "clouds": { "all": 48 }, "wind": { "speed": 0.76, "deg": 251.002 }, "rain": { "3h": 0.04 }, "sys": { "pod": "d" }, "dt_txt": "2018-06-23 12:00:00" }, { "dt": 1529766000, "main": { "temp": 294.815, "temp_min": 294.815, "temp_max": 294.815, "pressure": 874.28, "sea_level": 1020.19, "grnd_level": 874.28, "humidity": 58, "temp_kf": 0 }, "weather": [{ "id": 500, "main": "Rain", "description": "light rain", "icon": "10d" }], "clouds": { "all": 76 }, "wind": { "speed": 1.86, "deg": 352.501 }, "rain": { "3h": 0.185 }, "sys": { "pod": "d" }, "dt_txt": "2018-06-23 15:00:00" }, { "dt": 1529776800, "main": { "temp": 290.337, "temp_min": 290.337, "temp_max": 290.337, "pressure": 875.08, "sea_level": 1021.69, "grnd_level": 875.08, "humidity": 73, "temp_kf": 0 }, "weather": [{ "id": 500, "main": "Rain", "description": "light rain", "icon": "10n" }], "clouds": { "all": 36 }, "wind": { "speed": 0.76, "deg": 313.509 }, "rain": { "3h": 0.02 }, "sys": { "pod": "n" }, "dt_txt": "2018-06-23 18:00:00" }, { "dt": 1529787600, "main": { "temp": 288.89, "temp_min": 288.89, "temp_max": 288.89, "pressure": 875.39, "sea_level": 1022.49, "grnd_level": 875.39, "humidity": 81, "temp_kf": 0 }, "weather": [{ "id": 500, "main": "Rain", "description": "light rain", "icon": "10n" }], "clouds": { "all": 56 }, "wind": { "speed": 1.01, "deg": 40.0106 }, "rain": { "3h": 0.055 }, "sys": { "pod": "n" }, "dt_txt": "2018-06-23 21:00:00" }, { "dt": 1529798400, "main": { "temp": 287.14, "temp_min": 287.14, "temp_max": 287.14, "pressure": 875.88, "sea_level": 1023.4, "grnd_level": 875.88, "humidity": 88, "temp_kf": 0 }, "weather": [{ "id": 500, "main": "Rain", "description": "light rain", "icon": "10n" }], "clouds": { "all": 0 }, "wind": { "speed": 0.56, "deg": 269.504 }, "rain": { "3h": 0.0050000000000001 }, "sys": { "pod": "n" }, "dt_txt": "2018-06-24 00:00:00" }, { "dt": 1529809200, "main": { "temp": 287.812, "temp_min": 287.812, "temp_max": 287.812, "pressure": 876.56, "sea_level": 1024.3, "grnd_level": 876.56, "humidity": 86, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" }], "clouds": { "all": 0 }, "wind": { "speed": 0.71, "deg": 277.505 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-24 03:00:00" }, { "dt": 1529820000, "main": { "temp": 293.984, "temp_min": 293.984, "temp_max": 293.984, "pressure": 876.57, "sea_level": 1023.26, "grnd_level": 876.57, "humidity": 71, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" }], "clouds": { "all": 0 }, "wind": { "speed": 1.41, "deg": 234.503 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-24 06:00:00" }, { "dt": 1529830800, "main": { "temp": 298.045, "temp_min": 298.045, "temp_max": 298.045, "pressure": 875.39, "sea_level": 1021.2, "grnd_level": 875.39, "humidity": 63, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" }], "clouds": { "all": 0 }, "wind": { "speed": 1.62, "deg": 244.502 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-24 09:00:00" }, { "dt": 1529841600, "main": { "temp": 299.666, "temp_min": 299.666, "temp_max": 299.666, "pressure": 874.25, "sea_level": 1019.85, "grnd_level": 874.25, "humidity": 54, "temp_kf": 0 }, "weather": [{ "id": 500, "main": "Rain", "description": "light rain", "icon": "10d" }], "clouds": { "all": 0 }, "wind": { "speed": 0.77, "deg": 13.5015 }, "rain": { "3h": 0.11 }, "sys": { "pod": "d" }, "dt_txt": "2018-06-24 12:00:00" }, { "dt": 1529852400, "main": { "temp": 295.301, "temp_min": 295.301, "temp_max": 295.301, "pressure": 874.92, "sea_level": 1021.12, "grnd_level": 874.92, "humidity": 60, "temp_kf": 0 }, "weather": [{ "id": 500, "main": "Rain", "description": "light rain", "icon": "10d" }], "clouds": { "all": 80 }, "wind": { "speed": 1.51, "deg": 352.5 }, "rain": { "3h": 0.41 }, "sys": { "pod": "d" }, "dt_txt": "2018-06-24 15:00:00" }, { "dt": 1529863200, "main": { "temp": 291.351, "temp_min": 291.351, "temp_max": 291.351, "pressure": 877.27, "sea_level": 1024.08, "grnd_level": 877.27, "humidity": 69, "temp_kf": 0 }, "weather": [{ "id": 500, "main": "Rain", "description": "light rain", "icon": "10n" }], "clouds": { "all": 88 }, "wind": { "speed": 0.96, "deg": 202.001 }, "rain": { "3h": 0.16 }, "sys": { "pod": "n" }, "dt_txt": "2018-06-24 18:00:00" }, { "dt": 1529874000, "main": { "temp": 289.244, "temp_min": 289.244, "temp_max": 289.244, "pressure": 877.29, "sea_level": 1024.89, "grnd_level": 877.29, "humidity": 83, "temp_kf": 0 }, "weather": [{ "id": 500, "main": "Rain", "description": "light rain", "icon": "10n" }], "clouds": { "all": 12 }, "wind": { "speed": 0.86, "deg": 2.00073 }, "rain": { "3h": 0.245 }, "sys": { "pod": "n" }, "dt_txt": "2018-06-24 21:00:00" }, { "dt": 1529884800, "main": { "temp": 287.522, "temp_min": 287.522, "temp_max": 287.522, "pressure": 877.08, "sea_level": 1024.9, "grnd_level": 877.08, "humidity": 88, "temp_kf": 0 }, "weather": [{ "id": 500, "main": "Rain", "description": "light rain", "icon": "10n" }], "clouds": { "all": 80 }, "wind": { "speed": 0.75, "deg": 318.504 }, "rain": { "3h": 0.075 }, "sys": { "pod": "n" }, "dt_txt": "2018-06-25 00:00:00" }, { "dt": 1529895600, "main": { "temp": 288.908, "temp_min": 288.908, "temp_max": 288.908, "pressure": 877.54, "sea_level": 1025.36, "grnd_level": 877.54, "humidity": 83, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" }], "clouds": { "all": 0 }, "wind": { "speed": 0.05, "deg": 262.003 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-25 03:00:00" }, { "dt": 1529906400, "main": { "temp": 294.535, "temp_min": 294.535, "temp_max": 294.535, "pressure": 877.28, "sea_level": 1024.11, "grnd_level": 877.28, "humidity": 69, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" }], "clouds": { "all": 0 }, "wind": { "speed": 1.13, "deg": 219.503 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-25 06:00:00" }, { "dt": 1529917200, "main": { "temp": 298.43, "temp_min": 298.43, "temp_max": 298.43, "pressure": 876.1, "sea_level": 1022.05, "grnd_level": 876.1, "humidity": 62, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" }], "clouds": { "all": 0 }, "wind": { "speed": 0.47, "deg": 209.002 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-25 09:00:00" }, { "dt": 1529928000, "main": { "temp": 300.707, "temp_min": 300.707, "temp_max": 300.707, "pressure": 874.91, "sea_level": 1020.42, "grnd_level": 874.91, "humidity": 53, "temp_kf": 0 }, "weather": [{ "id": 500, "main": "Rain", "description": "light rain", "icon": "10d" }], "clouds": { "all": 8 }, "wind": { "speed": 1.12, "deg": 91.0061 }, "rain": { "3h": 0.085 }, "sys": { "pod": "d" }, "dt_txt": "2018-06-25 12:00:00" }, { "dt": 1529938800, "main": { "temp": 300.111, "temp_min": 300.111, "temp_max": 300.111, "pressure": 874.29, "sea_level": 1020.03, "grnd_level": 874.29, "humidity": 45, "temp_kf": 0 }, "weather": [{ "id": 500, "main": "Rain", "description": "light rain", "icon": "10d" }], "clouds": { "all": 8 }, "wind": { "speed": 1.41, "deg": 63.0023 }, "rain": { "3h": 0.0049999999999999 }, "sys": { "pod": "d" }, "dt_txt": "2018-06-25 15:00:00" }, { "dt": 1529949600, "main": { "temp": 295.261, "temp_min": 295.261, "temp_max": 295.261, "pressure": 874.79, "sea_level": 1021.26, "grnd_level": 874.79, "humidity": 45, "temp_kf": 0 }, "weather": [{ "id": 802, "main": "Clouds", "description": "scattered clouds", "icon": "03n" }], "clouds": { "all": 48 }, "wind": { "speed": 3.2, "deg": 35.0003 }, "rain": {}, "sys": { "pod": "n" }, "dt_txt": "2018-06-25 18:00:00" }, { "dt": 1529960400, "main": { "temp": 291.742, "temp_min": 291.742, "temp_max": 291.742, "pressure": 874.55, "sea_level": 1021.47, "grnd_level": 874.55, "humidity": 52, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01n" }], "clouds": { "all": 0 }, "wind": { "speed": 1.66, "deg": 32.0015 }, "rain": {}, "sys": { "pod": "n" }, "dt_txt": "2018-06-25 21:00:00" }, { "dt": 1529971200, "main": { "temp": 289.406, "temp_min": 289.406, "temp_max": 289.406, "pressure": 874.74, "sea_level": 1021.87, "grnd_level": 874.74, "humidity": 60, "temp_kf": 0 }, "weather": [{ "id": 802, "main": "Clouds", "description": "scattered clouds", "icon": "03n" }], "clouds": { "all": 36 }, "wind": { "speed": 1.36, "deg": 11.5036 }, "rain": {}, "sys": { "pod": "n" }, "dt_txt": "2018-06-26 00:00:00" }, { "dt": 1529982000, "main": { "temp": 290.344, "temp_min": 290.344, "temp_max": 290.344, "pressure": 875.24, "sea_level": 1022.61, "grnd_level": 875.24, "humidity": 68, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" }], "clouds": { "all": 0 }, "wind": { "speed": 1.07, "deg": 25.501 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-26 03:00:00" }, { "dt": 1529992800, "main": { "temp": 296.669, "temp_min": 296.669, "temp_max": 296.669, "pressure": 875.14, "sea_level": 1021.37, "grnd_level": 875.14, "humidity": 58, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" }], "clouds": { "all": 0 }, "wind": { "speed": 1.11, "deg": 225.5 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-26 06:00:00" }, { "dt": 1530003600, "main": { "temp": 300.4, "temp_min": 300.4, "temp_max": 300.4, "pressure": 873.55, "sea_level": 1019, "grnd_level": 873.55, "humidity": 52, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" }], "clouds": { "all": 0 }, "wind": { "speed": 1.12, "deg": 237.501 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-26 09:00:00" }, { "dt": 1530014400, "main": { "temp": 302.474, "temp_min": 302.474, "temp_max": 302.474, "pressure": 872.27, "sea_level": 1017.3, "grnd_level": 872.27, "humidity": 42, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" }], "clouds": { "all": 0 }, "wind": { "speed": 1.52, "deg": 234 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-26 12:00:00" }, { "dt": 1530025200, "main": { "temp": 300.919, "temp_min": 300.919, "temp_max": 300.919, "pressure": 872.19, "sea_level": 1017.17, "grnd_level": 872.19, "humidity": 37, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "02d" }], "clouds": { "all": 8 }, "wind": { "speed": 1.65, "deg": 266.501 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-26 15:00:00" }, { "dt": 1530036000, "main": { "temp": 294.477, "temp_min": 294.477, "temp_max": 294.477, "pressure": 872.56, "sea_level": 1018.52, "grnd_level": 872.56, "humidity": 55, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01n" }], "clouds": { "all": 0 }, "wind": { "speed": 0.92, "deg": 342.504 }, "rain": {}, "sys": { "pod": "n" }, "dt_txt": "2018-06-26 18:00:00" }, { "dt": 1530046800, "main": { "temp": 290.615, "temp_min": 290.615, "temp_max": 290.615, "pressure": 873.13, "sea_level": 1019.46, "grnd_level": 873.13, "humidity": 51, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01n" }], "clouds": { "all": 0 }, "wind": { "speed": 1.81, "deg": 20.505 }, "rain": {}, "sys": { "pod": "n" }, "dt_txt": "2018-06-26 21:00:00" }, { "dt": 1530057600, "main": { "temp": 288.302, "temp_min": 288.302, "temp_max": 288.302, "pressure": 873.42, "sea_level": 1020.19, "grnd_level": 873.42, "humidity": 68, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01n" }], "clouds": { "all": 0 }, "wind": { "speed": 1.42, "deg": 10.0019 }, "rain": {}, "sys": { "pod": "n" }, "dt_txt": "2018-06-27 00:00:00" }, { "dt": 1530068400, "main": { "temp": 290.209, "temp_min": 290.209, "temp_max": 290.209, "pressure": 873.93, "sea_level": 1020.72, "grnd_level": 873.93, "humidity": 65, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" }], "clouds": { "all": 0 }, "wind": { "speed": 1.66, "deg": 36 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-27 03:00:00" }, { "dt": 1530079200, "main": { "temp": 297.644, "temp_min": 297.644, "temp_max": 297.644, "pressure": 873.95, "sea_level": 1019.72, "grnd_level": 873.95, "humidity": 52, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" }], "clouds": { "all": 0 }, "wind": { "speed": 1.61, "deg": 230 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-27 06:00:00" }, { "dt": 1530090000, "main": { "temp": 301.518, "temp_min": 301.518, "temp_max": 301.518, "pressure": 873.16, "sea_level": 1018.11, "grnd_level": 873.16, "humidity": 45, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" }], "clouds": { "all": 0 }, "wind": { "speed": 0.51, "deg": 309.5 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-27 09:00:00" }, { "dt": 1530100800, "main": { "temp": 303.24, "temp_min": 303.24, "temp_max": 303.24, "pressure": 872.25, "sea_level": 1016.8, "grnd_level": 872.25, "humidity": 32, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" }], "clouds": { "all": 0 }, "wind": { "speed": 1.32, "deg": 304.5 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-27 12:00:00" }, { "dt": 1530111600, "main": { "temp": 302.008, "temp_min": 302.008, "temp_max": 302.008, "pressure": 871.75, "sea_level": 1016.41, "grnd_level": 871.75, "humidity": 33, "temp_kf": 0 }, "weather": [{ "id": 800, "main": "Clear", "description": "clear sky", "icon": "01d" }], "clouds": { "all": 0 }, "wind": { "speed": 0.75, "deg": 260.003 }, "rain": {}, "sys": { "pod": "d" }, "dt_txt": "2018-06-27 15:00:00" }], "city": { "id": 616052, "name": "Yerevan", "coord": { "lat": 40.1776, "lon": 44.5126 }, "country": "AM", "population": 1093485 } } How do I extract an item from this list? I need to use AESON. for example, how do I extract this? {"temp":288.89,"temp_min":288.89,"temp_max":288.89,"pressure":875.39,"sea_level":1022.49,"grnd_level":875.39,"humidity":81,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"clouds":{"all":56},"wind":{"speed":1.01,"deg":40.0106},"rain":{"3h":0.055},"sys":{"pod":"n"},"dt_txt":"2018-06-23 21:00:00"},{"dt":1529798400,"main": My code: {-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE OverloadedStrings #-} module PrepareAnswer where import Data.Aeson import qualified Data.ByteString.Lazy as BSL import Data.Maybe (fromMaybe) import Data.Text import Data.Time.Clock import GHC.Generics import Network.HTTP.Client data MainWeatherInfo = MainWeatherInfo { mainInfo :: MainInfo , list :: ListWeatherInfo } deriving (Show, Generic) data ListWeatherInfo = ListWeatherInfo { temper :: Double , temp_min :: Double , temp_max :: Double , pressure1 :: Double , sea_level :: Double , grnd_level :: Double , humidity :: Int , temp_kf :: Double , dt_txt :: UTCTime } deriving (Show, Generic) instance ToJSON MainWeatherInfo where toJSON = genericToJSON defaultOptions { fieldLabelModifier = Prelude.take 4 } instance FromJSON MainWeatherInfo where parseJSON = genericParseJSON defaultOptions { fieldLabelModifier = Prelude.take 4 } instance ToJSON ListWeatherInfo instance FromJSON ListWeatherInfo data MainInfo = MainInfo { temp :: Double , pressure :: Int } deriving ( Show , Generic , ToJSON , FromJSON ) prepareAnswer :: Response BSL.ByteString -> Text prepareAnswer response = Data.Text.pack finalPhrase where finalPhrase = prepareValues weatherValues weatherValues = extractValues . responseBody $ response extractValues :: BSL.ByteString -> MainWeatherInfo extractValues rawJSON = let result = decode rawJSON :: Maybe MainWeatherInfo in fromMaybe (error "Invalid JSON!") result prepareValues :: MainWeatherInfo -> String prepareValues (MainWeatherInfo dataWeNeed forecastList) = "The expected temperature is: " ++ show (temp dataWeNeed ) ++ " degrees Celsius, atmospheric pressure is " ++ show (pressure dataWeNeed) ++ " mm/hg"
You probably want list :: [ListWeatherInfo]. Currently it expects that list will be a record, but in the example data it is a json array.
Node-RED not able to access to array member
I'm trying to access to a JSON array returned by a OpenWeatherMap request. The JSON data is: { "city": { "id": 3171457, "name": "New York", "coord": { "lon": 0.32898, "lat": 4.802662 }, "country": "US", "population": 0, "sys": { "population": 0 } }, "cod": "200", "message": 0.0317, "cnt": 40, "list": [ { "dt": 1483552800, "main": { "temp": 277.28, "temp_min": 275.705, "temp_max": 277.28, "pressure": 1013.85, "sea_level": 1021.42, "grnd_level": 1013.85, "humidity": 93, "temp_kf": 1.57 }, "weather": [ { "id": 800, "main": "Clear", "description": "clear sky", "icon": "01n" } ], "clouds": { "all": 0 }, "wind": { "speed": 4.74, "deg": 269.002 }, "sys": { "pod": "n" }, "dt_txt": "2017-01-04 18:00:00" }, ... If i use in a template {{payload.list}} I see a list of [object Object]s. But if I use {{payload.list[0]}}, or {{payload.list[0].main}}, I see nothing - my guess was a single [object Object]. How can i access to the first member and inner members?
The template node uses the mustache format, documented here: https://mustache.github.io/mustache.5.html To access array values, you should use the following syntax: {{ payload.list.0 }} and {{ payload.list.0.main }}