简体   繁体   English

在 flutter 中解码字符串 json 时出错,“字符串类型不是 Map 类型的子类型”

[英]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.

相关问题 flutter JSON 解码未处理的异常:类型“空”不是类型“字符串”的子类型 - flutter JSON decoding Unhandled Exception: type 'Null' is not a subtype of type 'String' Flutter 中的 Json 解析:列表<dynamic >不是“地图”类型的子类型<String,dynamic> &#39; - Json Parsing in Flutter: List<dynamic > is not a subtype of type 'Map<String,dynamic>' 未处理的异常:“字符串”类型不是“地图”类型的子类型<String, dynamic> &#39; 将 JSON 转换为 Map 时<String, dynamic>在颤振中 - Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>' when converting JSON to a Map<String, dynamic> in Flutter Flutter 错误:列表<dynamic>不是 Map 类型的子类型<string, dynamic></string,></dynamic> - Flutter Error: List<dynamic> is not a subtype of type Map<String, dynamic> 错误 Flutter:列表<dynamic>不是类型 Map 的子类型<string, dynamic></string,></dynamic> - Error Flutter: List<dynamic> is not a subtype of type Map<String, dynamic> Flutter 显示嵌套在 ListView 中的 json 返回类型 String 不是类型 'Map 的子类型<string, dynamic> ' 在类型转换中</string,> - Flutter display nested json in ListView return type String is not a subtype of type 'Map<String, dynamic>' in type cast Flutter 中的错误:“字符串”类型不是“地图”类型的子类型<string, dynamic> ? 在类型转换中</string,> - Error in Flutter: type 'string' is not a subtype of type 'map<string, dynamic>?' in type cast 错误:“String”类型不是“Map”类型的子类型<String, dynamic> &#39; 在类型转换中 - Error : type 'String' is not a subtype of type 'Map<String, dynamic>' in type cast Flutter JSON Array Parsing 获取错误:异常:type &#39;(dynamic) =&gt; xxx is not a subtype of type &#39;(Map<String, dynamic> ) =&gt; 列表<xxx> &#39; 离开&#39; - Flutter JSON Array Parsing getting error : Exception: type '(dynamic) => xxx is not a subtype of type '(Map<String, dynamic>) => List<xxx>' of 'f' 未处理的异常:键入“列表”<dynamic> &#39; 不是类型 &#39;Map 的子类型<String, dynamic> &#39; 在颤动中如何像这样发布 json obj - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in flutter how to post json obj like this
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM