dart - 在 Dart 中将字符串转换为 map

标签 dart flutter

我想将字符串转换为 map 。

String value = "{first_name : fname,last_name : lname,gender : male, location : { state : state, country : country, place : place} }"

进入

Map = {
first_name : fname,
last_name : lname,
gender : male,
location = {
  state : state, 
  country : country, 
  place : place
 }
}

如何将字符串转换为 map<String, dynamic>其中 value 由 string、int、object 和 boolean 组成?

我想将字符串保存到文件中,并从文件中获取数据。

最佳答案

这是不可能的。

如果可以将字符串更改为有效的 JSON,则可以使用

import 'dart:convert';
...
Map valueMap = json.decode(value);
// or
Map valueMap = jsonDecode(value);

字符串需要看起来像

{"first_name" : "fname","last_name" : "lname","gender" : "male", "location" : { "state" : "state", "country" : "country", "place" : "place"} }

关于dart - 在 Dart 中将字符串转换为 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49651673/

相关文章:

ios - Flutter iOS 运行错误 : Could not parse callback cache, 中止恢复

dart - Flutter - 在底部绘制一个矩形

flutter - 在 Flutter 中实现折叠工具栏

flutter - 如何使用 'CupertinoFullscreenDialogTransition' ?

flutter - 点击障碍时小部件不会弹出

dart - 如何从抽屉导航 flutter 中重置任何类的状态?

regex - 正则表达式在imap响应中获取文件的base64字符串

dart - 为什么无法编译此简单的Dart代码?

使用 flogs 的 Flutter 日志 - 它将保存在哪里

flutter - Flutter DropDownbutton不显示所选值