json.decode 可以解析相同的字符串但不能解析 response.body
json.decode can parse same string but can not parse response.body
当我这样写代码时:
if (response.statusCode == ApiClient.successResponse &&
response.body != null) {
final Map<String, dynamic> parsed = jsonDecode(response.body);
}
它给我一个错误,说:未处理的异常:类型 'String' 不是
'Map<String, dynamic>'
类型的子类型
但是,如果我在这一行设置断点:
final Map<String, dynamic> parsed = jsonDecode(response.body);
并从 debug.console 复制 response.body 字符串,然后在新的测试项目中尝试此代码:
String s3 = "{\"scan_since\":\"\",\"total_scan\":0,\"total_scan_points\":0,\"total_medals_scan\":0,\"total_medals_play\":0,\"list_medal_checkin\":[{\"count\":0,\"encouragement_id\":\"STRENC0104\",\"encouragement_msg\":\"Well done!\",\"code\":\"MEDALDAYS1\",\"name\":\"Super Starter\",\"qualification\":\"Use Scanutt to check-in for the first time\",\"repeatable\":false,\"points\":10,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0104\",\"encouragement_msg\":\"Well done!\",\"code\":\"MEDALDAYS2\",\"name\":\"Double-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 2 days consecutively for the first time\",\"repeatable\":false,\"points\":10,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0102\",\"encouragement_msg\":\"Good work!\",\"code\":\"MEDALDAYS5\",\"name\":\"5-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 5 days consecutively for the first time\",\"repeatable\":false,\"points\":15,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0104\",\"encouragement_msg\":\"Well done!\",\"code\":\"MEDALDAYS7\",\"name\":\"7-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 7 days consecutively for the first time\",\"repeatable\":false,\"points\":20,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0102\",\"encouragement_msg\":\"Good work!\",\"code\":\"MEDALDAYS15\",\"name\":\"15-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 15 days consecutively for the first time\",\"repeatable\":false,\"points\":50,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0204\",\"encouragement_msg\":\"You Rock!\",\"code\":\"MEDALDAYS30\",\"name\":\"30-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 30 days consecutively for the first time\",\"repeatable\":false,\"points\":100,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0202\",\"encouragement_msg\":\"You are Fantastic!\",\"code\":\"MEDALDAYS60\",\"name\":\"60-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 60 days consecutively for the first time\",\"repeatable\":false,\"points\":500,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0204\",\"encouragement_msg\":\"You Rock!\",\"code\":\"MEDALDAYS90\",\"name\":\"90-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 90 days consecutively for the first time\",\"repeatable\":false,\"points\":1000,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0402\",\"encouragement_msg\":\"You are dazzling!\",\"code\":\"MEDALDAYS180\",\"name\":\"180-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 180 days consecutively for the first time\",\"repeatable\":false,\"points\":2500,\"level\":4},{\"count\":0,\"encouragement_id\":\"STRENC0402\",\"encouragement_msg\":\"You are dazzling!\",\"code\":\"MEDALDAYS365\",\"name\":\"365-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 365 days consecutively for the first time\",\"repeatable\":false,\"points\":10000,\"level\":4},{\"count\":0,\"encouragement_id\":\"STRENC0308\",\"encouragement_msg\":\"You are Super!\",\"code\":\"MEDALWEEKS3\",\"name\":\"Triple Weekly Striker\",\"qualification\":\"Use Scanutt to check-in at least 3 times in a particular week (Monday-Sunday)\",\"repeatable\":true,\"points\":20,\"level\":3},{\"count\":0,\"encouragement_id\":\"STRENC0309\",\"encouragement_msg\":\"You Rock!\",\"code\":\"MEDALWEEKS5\",\"name\":\"Five Weekly Striker\",\"qualification\":\"Use Scanutt to check-in at least 5 times in a particular week (Monday-Sunday)\",\"repeatable\":true,\"points\":25,\"level\":3},{\"count\":0,\"encouragement_id\":\"STRENC0309\",\"encouragement_msg\":\"You Rock!\",\"code\":\"MEDALWEEKS7\",\"name\":\"Full Weekly Striker\",\"qualification\":\"Use Scanutt to check-in at least 7 times in a particular week (Monday-Sunday)\",\"repeatable\":true,\"points\":50,\"level\":3},{\"count\":0,\"encouragement_id\":\"STRENC0102\",\"encouragement_msg\":\"Good work!\",\"code\":\"MEDALTOTAL5\",\"name\":\"5 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 5 times\",\"repeatable\":false,\"points\":10,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0102\",\"encouragement_msg\":\"Good work!\",\"code\":\"MEDALTOTAL10\",\"name\":\"10 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 10 times\",\"repeatable\":false,\"points\":10,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0102\",\"encouragement_msg\":\"Good work!\",\"code\":\"MEDALTOTAL20\",\"name\":\"20 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 20 times\",\"repeatable\":false,\"points\":10,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0103\",\"encouragement_msg\":\"Keep it up!\",\"code\":\"MEDALTOTAL30\",\"name\":\"30 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 30 times\",\"repeatable\":false,\"points\":10,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0302\",\"encouragement_msg\":\"Good work!\",\"code\":\"MEDALTOTAL50\",\"name\":\"50 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 50 times\",\"repeatable\":false,\"points\":10,\"level\":3},{\"count\":0,\"encouragement_id\":\"STRENC0302\",\"encouragement_msg\":\"Good work!\",\"code\":\"MEDALTOTAL70\",\"name\":\"70 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 70 times\",\"repeatable\":false,\"points\":10,\"level\":3},{\"count\":0,\"encouragement_id\":\"STRENC0306\",\"encouragement_msg\":\"Impressive!\",\"code\":\"MEDALTOTAL100\",\"name\":\"Hundred Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 100 times\",\"repeatable\":false,\"points\":100,\"level\":3},{\"count\":0,\"encouragement_id\":\"STRENC0201\",\"encouragement_msg\":\"Impressive!\",\"code\":\"MEDALTOTAL125\",\"name\":\"125 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 125 times\",\"repeatable\":false,\"points\":30,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0101\",\"encouragement_msg\":\"Great work!\",\"code\":\"MEDALTOTAL150\",\"name\":\"150 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 150 times\",\"repeatable\":false,\"points\":30,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0306\",\"encouragement_msg\":\"Impressive!\",\"code\":\"MEDALTOTAL175\",\"name\":\"175 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 175 times\",\"repeatable\":false,\"points\":30,\"level\":3},{\"count\":0,\"encouragement_id\":\"STRENC0201\",\"encouragement_msg\":\"Impressive!\",\"code\":\"MEDALTOTAL200\",\"name\":\"200 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 200 times\",\"repeatable\":false,\"points\":100,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0301\",\"encouragement_msg\":\"Great work!\",\"code\":\"MEDALTOTAL250\",\"name\":\"250 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 250 times\",\"repeatable\":false,\"points\":80,\"level\":3},{\"count\":0,\"encouragement_id\":\"STRENC0201\",\"encouragement_msg\":\"Impressive!\",\"code\":\"MEDALTOTAL300\",\"name\":\"300 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 300 times\",\"repeatable\":false,\"points\":100,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0201\",\"encouragement_msg\":\"Impressive!\",\"code\":\"MEDALTOTAL350\",\"name\":\"350 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 350 times\",\"repeatable\":false,\"points\":80,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0204\",\"encouragement_msg\":\"You Rock!\",\"code\":\"MEDALTOTAL400\",\"name\":\"400 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 400 times\",\"repeatable\":false,\"points\":150,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0202\",\"encouragement_msg\":\"You are Fantastic!\",\"code\":\"MEDALTOTAL450\",\"name\":\"450 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 450 times\",\"repeatable\":false,\"points\":100,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0402\",\"encouragement_msg\":\"You are dazzling!\",\"code\":\"MEDALTOTAL500\",\"name\":\"500 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 500 times\",\"repeatable\":false,\"points\":200,\"level\":4},{\"count\":0,\"encouragement_id\":\"STRENC0404\",\"encouragement_msg\":\"You are Amazing!\",\"code\":\"MEDALTOTAL1000\",\"name\":\"1000 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 1000 times\",\"repeatable\":false,\"points\":500,\"level\":4}],\"code\":0,\"message\":\"\"}";
// print(json.decode(s1));
final Map<String, dynamic> parsed = json.decode(s3);
它工作正常,那么 response.body 有什么问题?不是字符串吗?
你从 api 得到的是一个字符串,所以你必须说
if (response.statusCode == ApiClient.successResponse &&
response.body != null) {
var parsed = jsonDecode(response.body);
}
当我这样写代码时:
if (response.statusCode == ApiClient.successResponse &&
response.body != null) {
final Map<String, dynamic> parsed = jsonDecode(response.body);
}
它给我一个错误,说:未处理的异常:类型 'String' 不是 'Map<String, dynamic>'
类型的子类型但是,如果我在这一行设置断点:
final Map<String, dynamic> parsed = jsonDecode(response.body);
并从 debug.console 复制 response.body 字符串,然后在新的测试项目中尝试此代码:
String s3 = "{\"scan_since\":\"\",\"total_scan\":0,\"total_scan_points\":0,\"total_medals_scan\":0,\"total_medals_play\":0,\"list_medal_checkin\":[{\"count\":0,\"encouragement_id\":\"STRENC0104\",\"encouragement_msg\":\"Well done!\",\"code\":\"MEDALDAYS1\",\"name\":\"Super Starter\",\"qualification\":\"Use Scanutt to check-in for the first time\",\"repeatable\":false,\"points\":10,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0104\",\"encouragement_msg\":\"Well done!\",\"code\":\"MEDALDAYS2\",\"name\":\"Double-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 2 days consecutively for the first time\",\"repeatable\":false,\"points\":10,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0102\",\"encouragement_msg\":\"Good work!\",\"code\":\"MEDALDAYS5\",\"name\":\"5-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 5 days consecutively for the first time\",\"repeatable\":false,\"points\":15,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0104\",\"encouragement_msg\":\"Well done!\",\"code\":\"MEDALDAYS7\",\"name\":\"7-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 7 days consecutively for the first time\",\"repeatable\":false,\"points\":20,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0102\",\"encouragement_msg\":\"Good work!\",\"code\":\"MEDALDAYS15\",\"name\":\"15-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 15 days consecutively for the first time\",\"repeatable\":false,\"points\":50,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0204\",\"encouragement_msg\":\"You Rock!\",\"code\":\"MEDALDAYS30\",\"name\":\"30-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 30 days consecutively for the first time\",\"repeatable\":false,\"points\":100,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0202\",\"encouragement_msg\":\"You are Fantastic!\",\"code\":\"MEDALDAYS60\",\"name\":\"60-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 60 days consecutively for the first time\",\"repeatable\":false,\"points\":500,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0204\",\"encouragement_msg\":\"You Rock!\",\"code\":\"MEDALDAYS90\",\"name\":\"90-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 90 days consecutively for the first time\",\"repeatable\":false,\"points\":1000,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0402\",\"encouragement_msg\":\"You are dazzling!\",\"code\":\"MEDALDAYS180\",\"name\":\"180-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 180 days consecutively for the first time\",\"repeatable\":false,\"points\":2500,\"level\":4},{\"count\":0,\"encouragement_id\":\"STRENC0402\",\"encouragement_msg\":\"You are dazzling!\",\"code\":\"MEDALDAYS365\",\"name\":\"365-in-a-Rower\",\"qualification\":\"Use Scanutt to check-in 365 days consecutively for the first time\",\"repeatable\":false,\"points\":10000,\"level\":4},{\"count\":0,\"encouragement_id\":\"STRENC0308\",\"encouragement_msg\":\"You are Super!\",\"code\":\"MEDALWEEKS3\",\"name\":\"Triple Weekly Striker\",\"qualification\":\"Use Scanutt to check-in at least 3 times in a particular week (Monday-Sunday)\",\"repeatable\":true,\"points\":20,\"level\":3},{\"count\":0,\"encouragement_id\":\"STRENC0309\",\"encouragement_msg\":\"You Rock!\",\"code\":\"MEDALWEEKS5\",\"name\":\"Five Weekly Striker\",\"qualification\":\"Use Scanutt to check-in at least 5 times in a particular week (Monday-Sunday)\",\"repeatable\":true,\"points\":25,\"level\":3},{\"count\":0,\"encouragement_id\":\"STRENC0309\",\"encouragement_msg\":\"You Rock!\",\"code\":\"MEDALWEEKS7\",\"name\":\"Full Weekly Striker\",\"qualification\":\"Use Scanutt to check-in at least 7 times in a particular week (Monday-Sunday)\",\"repeatable\":true,\"points\":50,\"level\":3},{\"count\":0,\"encouragement_id\":\"STRENC0102\",\"encouragement_msg\":\"Good work!\",\"code\":\"MEDALTOTAL5\",\"name\":\"5 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 5 times\",\"repeatable\":false,\"points\":10,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0102\",\"encouragement_msg\":\"Good work!\",\"code\":\"MEDALTOTAL10\",\"name\":\"10 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 10 times\",\"repeatable\":false,\"points\":10,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0102\",\"encouragement_msg\":\"Good work!\",\"code\":\"MEDALTOTAL20\",\"name\":\"20 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 20 times\",\"repeatable\":false,\"points\":10,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0103\",\"encouragement_msg\":\"Keep it up!\",\"code\":\"MEDALTOTAL30\",\"name\":\"30 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 30 times\",\"repeatable\":false,\"points\":10,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0302\",\"encouragement_msg\":\"Good work!\",\"code\":\"MEDALTOTAL50\",\"name\":\"50 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 50 times\",\"repeatable\":false,\"points\":10,\"level\":3},{\"count\":0,\"encouragement_id\":\"STRENC0302\",\"encouragement_msg\":\"Good work!\",\"code\":\"MEDALTOTAL70\",\"name\":\"70 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 70 times\",\"repeatable\":false,\"points\":10,\"level\":3},{\"count\":0,\"encouragement_id\":\"STRENC0306\",\"encouragement_msg\":\"Impressive!\",\"code\":\"MEDALTOTAL100\",\"name\":\"Hundred Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 100 times\",\"repeatable\":false,\"points\":100,\"level\":3},{\"count\":0,\"encouragement_id\":\"STRENC0201\",\"encouragement_msg\":\"Impressive!\",\"code\":\"MEDALTOTAL125\",\"name\":\"125 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 125 times\",\"repeatable\":false,\"points\":30,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0101\",\"encouragement_msg\":\"Great work!\",\"code\":\"MEDALTOTAL150\",\"name\":\"150 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 150 times\",\"repeatable\":false,\"points\":30,\"level\":1},{\"count\":0,\"encouragement_id\":\"STRENC0306\",\"encouragement_msg\":\"Impressive!\",\"code\":\"MEDALTOTAL175\",\"name\":\"175 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 175 times\",\"repeatable\":false,\"points\":30,\"level\":3},{\"count\":0,\"encouragement_id\":\"STRENC0201\",\"encouragement_msg\":\"Impressive!\",\"code\":\"MEDALTOTAL200\",\"name\":\"200 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 200 times\",\"repeatable\":false,\"points\":100,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0301\",\"encouragement_msg\":\"Great work!\",\"code\":\"MEDALTOTAL250\",\"name\":\"250 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 250 times\",\"repeatable\":false,\"points\":80,\"level\":3},{\"count\":0,\"encouragement_id\":\"STRENC0201\",\"encouragement_msg\":\"Impressive!\",\"code\":\"MEDALTOTAL300\",\"name\":\"300 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 300 times\",\"repeatable\":false,\"points\":100,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0201\",\"encouragement_msg\":\"Impressive!\",\"code\":\"MEDALTOTAL350\",\"name\":\"350 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 350 times\",\"repeatable\":false,\"points\":80,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0204\",\"encouragement_msg\":\"You Rock!\",\"code\":\"MEDALTOTAL400\",\"name\":\"400 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 400 times\",\"repeatable\":false,\"points\":150,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0202\",\"encouragement_msg\":\"You are Fantastic!\",\"code\":\"MEDALTOTAL450\",\"name\":\"450 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 450 times\",\"repeatable\":false,\"points\":100,\"level\":2},{\"count\":0,\"encouragement_id\":\"STRENC0402\",\"encouragement_msg\":\"You are dazzling!\",\"code\":\"MEDALTOTAL500\",\"name\":\"500 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 500 times\",\"repeatable\":false,\"points\":200,\"level\":4},{\"count\":0,\"encouragement_id\":\"STRENC0404\",\"encouragement_msg\":\"You are Amazing!\",\"code\":\"MEDALTOTAL1000\",\"name\":\"1000 Achiever\",\"qualification\":\"Use Scanutt to check-in for a total of 1000 times\",\"repeatable\":false,\"points\":500,\"level\":4}],\"code\":0,\"message\":\"\"}";
// print(json.decode(s1));
final Map<String, dynamic> parsed = json.decode(s3);
它工作正常,那么 response.body 有什么问题?不是字符串吗?
你从 api 得到的是一个字符串,所以你必须说
if (response.statusCode == ApiClient.successResponse &&
response.body != null) {
var parsed = jsonDecode(response.body);
}