Reputation: 109
I am trying to fetch the name and price from JSON data as shown below but it only gives me one name and one price as I have to use the index to fetch a particular data how can I fetch all the available name and price from the JSON data
here is my function
void getData() async {
http.Response response = await http.get(
"API LINK");
if (response.statusCode == 200) {
String data = response.body;
var decodeData = jsonDecode(data);
var name = decodeData['entry'][0]['gsx\$name'];
var price = decodeData['entry'][0]['gsx\$price'];
print(name);
print(price);
}
}
Upvotes: 0
Views: 77
Reputation: 318
decodeData.forEach((key,value){
print(value['gsx\$name']);
print(value['gsx\$price']);
})
This can help.
Upvotes: 0
Reputation: 587
you can just use a for loop
for(var i = 0; i < decodeData['entry'].length; i++){
print(decodeData['entry'][i]['gsx\$name']);
print(decodeData['entry'][i]['gsx\$price']);
}
Upvotes: 1