Reputation: 3092
Recently I change all JSON of my app to show the errors, messages, and body of the service. In the body, I have an array of data. Before I change the JSON, all worked doing something like this:
final responseJson = json.decode(response.body);
Which returned:
[{"id":1,"descripcion":"Terreno Rio"},{"id":2,"descripcion":"Terreno Asier"}]
Now I try to do something like this:
final responseJson = json.decode(response.body);
print(json.encode(responseJson));
Which returns:
[{"code":0,"message":"","body":[{"id":1,"descripcion":"Terreno Rio"},{"id":2,"descripcion":"Terreno Asier"}]}]
Does anybody know the right way to extract some element of the JSON and decode?
Upvotes: 4
Views: 4125
Reputation: 103351
I'm sure the JSON response that you get is like this:
{"code":0,"message":"","body":[{"id":1,"descripcion":"Terreno Rio"},{"id":2,"descripcion":"Terreno Asier"}]}
So in order to parse that JSON, you can just access the body directly:
List list = responseJson['body'];
Now you can iterate through the elements of the array:
for (Map<String, dynamic> element in list) {
print(element);
}
Upvotes: 1
Reputation: 657068
You get a List
of Maps
. First access the first element (there is only 1) of the List
with [0]
and then the body
element of the returned Map
with ['body']
:
var body = responseJson[0]['body'];
print(body);
Upvotes: 0