Итерировать Json разобранный HTTP-ответ во флаттере - PullRequest
0 голосов
/ 06 августа 2020

Я получаю HTTP-ответ. Потом разбираю. Моя проблема в том, как выполнить итерацию проанализированного ответа json в for l oop

     final response =
     await http.get('http://10.0.2.2:8080/api/getetab');
     if (response.statusCode == 200) {
       var parsedJson = json.decode(response.body);
       print(parsedJson) ;

       return parsedJson ;
     } else {
       throw Exception('Failed to load');
     }

это результат анализа Json печати:

[{id: 1, nom: violette, adresse: tunis, categorie: coiffeuse, createdAt: 2020-08-05T12:10:10.000Z, updatedAt: 2020-08-05T12:10:10.000Z}, {id: 2, nom: soho, adresse: ariena, categorie: coiffeuse, createdAt: 2020-08-05T12:10:10.000Z, updatedAt: 2020-08-05T12:10:10.000Z}]

Как для итерации 'parsed Json' в для l oop?

1 Ответ

0 голосов
/ 07 августа 2020
  1. Полагаю, вы пропустили кавычки в json. Должно выглядеть так:
"[{\"id\": 1, \"nom\": \"violette\", \"adresse\": \"tunis\", \"categorie\": \"coiffeuse\", \"createdAt\": \"2020-08-05T12:10:10.000Z\", \"updatedAt\": \"2020-08-05T12:10:10.000Z\"}, {\"id\": 2, \"nom\": \"soho\", \"adresse\": \"ariena\", \"categorie\": \"coiffeuse\", \"createdAt\": \"2020-08-05T12:10:10.000Z\", \"updatedAt\": \"2020-08-05T12:10:10.000Z\"}]";
Когда вы поместите этот json выше в метод json.decode(String), он вернет вам List<Map<String, dynamic>>. Вы можете перебрать это с помощью простого forEach -L oop.
String jsonString =
        "[{\"id\": 1, \"nom\": \"violette\", \"adresse\": \"tunis\", \"categorie\": \"coiffeuse\", \"createdAt\": \"2020-08-05T12:10:10.000Z\", \"updatedAt\": \"2020-08-05T12:10:10.000Z\"}, {\"id\": 2, \"nom\": \"soho\", \"adresse\": \"ariena\", \"categorie\": \"coiffeuse\", \"createdAt\": \"2020-08-05T12:10:10.000Z\", \"updatedAt\": \"2020-08-05T12:10:10.000Z\"}]";
    

List<dynamic> data = json.decode(jsonString);

data.forEach((entry) {
  int id = entry["id"];
  String nom = entry["nom"];
  String adresse = entry["adresse"];

  print("id: $id, nom: $nom, adresse: $adresse");
});

...