Flutter - Json.decode return incorrect json

2,722

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);
}
Share:
2,722
El Hombre Sin Nombre
Author by

El Hombre Sin Nombre

Updated on December 07, 2022

Comments

  • El Hombre Sin Nombre
    El Hombre Sin Nombre over 1 year

    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?