Как разобрать массив строк в флаттере? - PullRequest
0 голосов
/ 23 февраля 2019

Мне нужно проанализировать массив строк, который не обязательно является допустимым JSON.

Я использую клиент HTTP и получаю следующее тело ответа от API.Как бы я сделал это во флаттере?

[
"Feb 22, 2019 20:16:04 UTC - TestProperty, Elevator 1001: The monitoring device on the elevator experienced a power loss and just restarted.",
"Feb 22, 2019 20:12:56 UTC - TestProperty, Elevator 1001: The monitoring device on the elevator experienced a power loss and just restarted.",
"Feb 22, 2019 19:20:33 UTC - TestProperty, Elevator 1001: The monitoring device on the elevator experienced a power loss and just restarted.",
"Feb 22, 2019 19:17:46 UTC - TestProperty, Elevator 1003: The monitoring device on the elevator experienced a power loss and just restarted.",
"Feb 22, 2019 18:35:36 UTC - TestProperty, Elevator 1001: The monitoring device on the elevator experienced a power loss and just restarted.",
"Feb 22, 2019 18:34:59 UTC - TestProperty, Elevator 1001: The monitoring device on the elevator experienced a power loss and just restarted.",
"Feb 22, 2019 18:34:59 UTC - TestProperty, Elevator 1001: The monitoring device on the elevator experienced a power loss and just restarted.",
"Feb 22, 2019 03:02:08 UTC - TestProperty, Elevator 1001: LiftAI is continuing to watch for any activity on this elevator, nothing during the past 15 minutes",
"Feb 22, 2019 02:47:04 UTC - TestProperty, Elevator 1001: LiftAI is continuing to watch for any activity on this elevator, nothing during the past 15 minutes",
"Feb 22, 2019 02:31:59 UTC - TestProperty, Elevator 1001: LiftAI is continuing to watch for any activity on this elevator, nothing during the past 15 minutes",
"Feb 22, 2019 02:16:54 UTC - TestProperty, Elevator 1001: LiftAI is continuing to watch for any activity on this elevator, nothing during the past 15 minutes",
"Feb 22, 2019 02:01:49 UTC - TestProperty, Elevator 1001: LiftAI is continuing to watch for any activity on this elevator, nothing during the past 15 minutes",
"Feb 22, 2019 01:46:44 UTC - TestProperty, Elevator 1001: LiftAI is continuing to watch for any activity on this elevator, nothing during the past 15 minutes",
"Feb 22, 2019 01:31:39 UTC - TestProperty, Elevator 1001: LiftAI is continuing to watch for any activity on this elevator, nothing during the past 15 minutes",
"Feb 22, 2019 01:16:34 UTC - TestProperty, Elevator 1001: Elevator just ended a trip up with a duration of 8 seconds",
"Feb 22, 2019 01:14:47 UTC - TestProperty, Elevator 1001: LiftAI is now watching the elevator, which has never recorded a trip",
"Feb 22, 2019 01:11:33 UTC - TestProperty, Elevator 1001: LiftAI is now watching the elevator, which has never recorded a trip"
]

1 Ответ

0 голосов
/ 23 февраля 2019

Это был глупый вопрос.Кто-то на Reddit упомянул, что массив строк является допустимым JSON.В следующем фрагменте показано, как преобразовать ответ в список строк, а затем создать экземпляр списка другого типа.

  static List<LiftNotification> parseProperties(String responseBody) {
    List<String> notifications = json.decode(responseBody).cast<String>();
    return notifications.map<LiftNotification>((notification) => new LiftNotification(name: notification)).toList();
  }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...