【问题标题】:Dart:convert fails to decode JSON string: TypeErrorDart:convert 无法解码 JSON 字符串:TypeError
【发布时间】:2023-04-08 16:21:01
【问题描述】:

我通过 API 请求得到以下字符串:

{
    "id": 21,
    "description": null,
    "creationDate": "2020-01-20T00:00:00",
    "status": "string",
    "open": false,
    "confirmedBy": null,
    "solvedDate": "0001-01-01T00:00:00",
    "repairedBy": null,
    "works": false,
    "brokenParts": [],
    "wellId": 9
}

现在,我尝试从这里创建一个对象:

var result = json.decode(response.body);

我收到以下错误:

type '_TypeError' is not a subtype of type 'String'

为什么会这样? JSON 有效。

【问题讨论】:

    标签: json flutter dart


    【解决方案1】:

    当从在线资源接收信息时,它实际上不是您接收的字符串变量(即使它可以以这种方式打印,但它的格式不正确)。它实际上是所谓的“响应”变量。您需要将其转换为 Map 才能将其分解为字符串。

    例如:

    Map result = jsonDecode(response.body);
    String tempString = result['id'];
    

    会得到地图内“id”变量的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多