[英]Error decoding string json in flutter, “type String is not subtype of type Map”
again with some doubts in flutter, I need to decode a json that I receive from an api, but it gives me a conversion error, it is because the json comes as a deserialized string, some way to solve this detail. again with some doubts in flutter, I need to decode a json that I receive from an api, but it gives me a conversion error, it is because the json comes as a deserialized string, some way to solve this detail. Thank you very much for the help.
非常感谢你的帮助。
String strVar = "{ \"status\": \"1\", \"message\": \"test\",
\"cars\": [ { \"carId\": \"1\", \"carName\": \"Car N°1\" },
{ \"carId\": \"2\", \"carName\": \"Car N°1\" },
{ \"businessId\": \"3\", \"carName\": \"Car N°1\" }
] }"
//Here error
Map<String,dynamic> mapAPI = json.decode(strVar);
To use multi-line string literals, encase the string in triple quotations '''
, or use only one line.要使用多行字符串文字,请将字符串括在三引号
'''
中,或仅使用一行。
Single-line单线
String strVar = "{ \"status\": \"1\", \"message\": \"test\",\"cars\": [ { \"carId\":\"1\", \"carName\": \"Car N°1\" }, { \"carId\": \"2\", \"carName\": \"Car N°1\" }, { \"businessId\": \"3\", \"carName\": \"Car N°1\" }] }";
Map<String, dynamic> map = jsonDecode(strVar);
print(map);
Multi-line多线
String strVar = '''{ \"status\": \"1\", \"message\": \"test\",
\"cars\": [ { \"carId\": \"1\", \"carName\": \"Car N°1\" },
{ \"carId\": \"2\", \"carName\": \"Car N°1\" },
{ \"businessId\": \"3\", \"carName\": \"Car N°1\" }
] }'''; // String encased in 3 single-quotations at start and end
Map<String, dynamic> map = jsonDecode(strVar);
print(map);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.