Flutter jsonDecode 返回 String 而不是 List

Bas*_*sam 2 json dart flutter

我想将 json 解码为列表,但jsonDecode返回字符串而不是列表。

我的 JSON:

[{
    "TicketID": 31,
    "EmpID": "11553",
    "Name": "Test",
    "Location": null,
    "PhoneExt": 345345,
    "Code": null,
    "Reason": null,
    "Category": null,
    "Created": null,
    "Username": "abc",
    "OtherLocation": null,
    "Room": null,
    "Floor": null,
    "CodeBlueDone": null,
    "CodeBlueDoneDate": null,
    "LocationCode": null,
    "PatientType": null,
    "EmergencyType": "Emergency",
    "FilledDateTime": null,
    "SubmitDateTime": null,
    "Type": null,
    "CallTime": "2022-08-26T13:43:25.003",
    "Status": "New"
}, {
    "TicketID": 30,
    "EmpID": "12",
    "Name": "dbdb",
    "Location": null,
    "PhoneExt": 123,
    "Code": null,
    "Reason": null,
    "Category": null,
    "Created": null,
    "Username": "abc",
    "OtherLocation": null,
    "Room": null,
    "Floor": null,
    "CodeBlueDone": null,
    "CodeBlueDoneDate": null,
    "LocationCode": null,
    "PatientType": null,
    "EmergencyType": "Emergency",
    "FilledDateTime": null,
    "SubmitDateTime": null,
    "Type": null,
    "CallTime": "2022-08-25T21:14:39.807",
    "Status": "New"
}]
Run Code Online (Sandbox Code Playgroud)
[{
    "TicketID": 31,
    "EmpID": "11553",
    "Name": "Test",
    "Location": null,
    "PhoneExt": 345345,
    "Code": null,
    "Reason": null,
    "Category": null,
    "Created": null,
    "Username": "abc",
    "OtherLocation": null,
    "Room": null,
    "Floor": null,
    "CodeBlueDone": null,
    "CodeBlueDoneDate": null,
    "LocationCode": null,
    "PatientType": null,
    "EmergencyType": "Emergency",
    "FilledDateTime": null,
    "SubmitDateTime": null,
    "Type": null,
    "CallTime": "2022-08-26T13:43:25.003",
    "Status": "New"
}, {
    "TicketID": 30,
    "EmpID": "12",
    "Name": "dbdb",
    "Location": null,
    "PhoneExt": 123,
    "Code": null,
    "Reason": null,
    "Category": null,
    "Created": null,
    "Username": "abc",
    "OtherLocation": null,
    "Room": null,
    "Floor": null,
    "CodeBlueDone": null,
    "CodeBlueDoneDate": null,
    "LocationCode": null,
    "PatientType": null,
    "EmergencyType": "Emergency",
    "FilledDateTime": null,
    "SubmitDateTime": null,
    "Type": null,
    "CallTime": "2022-08-25T21:14:39.807",
    "Status": "New"
}]
Run Code Online (Sandbox Code Playgroud)
if (response.statusCode == 200) {
  jsonDecode(response.body);
  print(jsonDecode(response.body).runtimeType);
}
Run Code Online (Sandbox Code Playgroud)

上面给出了完整的解码代码

为什么 flutter jsonDecode 或 json.Decode 不从上面的 JSON 返回列表?

编辑

调用 jsonDecode 两次有效并返回 List

Future<List> _getDataFromWeb() async {
  try {
    await http.post(
      Uri.parse(apiURL),
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      },
    ).then((response) {
      if (response.statusCode == 200) {
        try {
          final resp = jsonDecode(response.body) as List?;
          print(resp.runtimeType);
        } catch (ex) {
          print("_getDataFromWeb() error: ${ex}");
        }
      } else if (response.statusCode == 404) {
        print("Error 404: ${response.statusCode}");
      } else {
        print("Error: ${response.statusCode}");
      }
    }).catchError((error) {
      print("Error: " + error);
    });
  } catch (ex) {
    print("API-ERR: $ex");
  }
}
Run Code Online (Sandbox Code Playgroud)

我想知道为什么需要调用 jsonDecode() 两次?

Bas*_*sam 6

调用 jsonDecode 两次有效并返回 List

final resp = jsonDecode(jsonDecode(response.body));
print(resp.runtimeType);
Run Code Online (Sandbox Code Playgroud)

在投入数小时后终于找到了答案。