【问题标题】:How to parse an array of strings in flutter?如何在颤动中解析字符串数组?
【发布时间】:2019-02-23 01:09:29
【问题描述】:

我需要解析一个不一定是有效 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"
]

【问题讨论】:

  • 我假设您无法控制后端?你能解释为什么响应不一定是有效的 JSON 吗?是单引号还是双引号,尾随逗号等?

标签: http parsing flutter


【解决方案1】:

这是一个愚蠢的问题。 reddit 上有人提到字符串数组是有效的 JSON。下面的 sn-p 展示了如何将响应转换为字符串列表,然后实例化另一个类型的列表。

  static List<LiftNotification> parseProperties(String responseBody) {
    List<String> notifications = json.decode(responseBody).cast<String>();
    return notifications.map<LiftNotification>((notification) => new LiftNotification(name: notification)).toList();
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-13
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多