如何解析颤动中的字符串数组?

时间:2019-02-23 01:09:29

标签: http parsing flutter

我需要解析一个不一定是有效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 :(得分:0)

这是一个愚蠢的问题。 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();
  }