flutter中json、Map、List相互转换
json 转 List
String jsonStr = '[{"name":"budaye", "city":"北京"},{"name":"卜大爷", "city":"北京"}]';
List list = json.decode(jsonStr);
json 转 Map
上例中的 list 中,存储的就是 Map 对象:
//接着上例:
for(int i=0;i<list.length;i++){
Map map = list[i];
}
如果 json 对象不是一个 json 数组:
String jsonStr = '{"name":"budaye", "city":"北京"}';
Map<String, dynamic> map = json.decode(jsonStr);
List 转 json
String jsonStr = json.encode(list);
Map 转 json
String jsonStr = json.encode(map);
**PS:更多精彩内容,请查看 --> 《Flutter 开发》
**PS:更多精彩内容,请查看 --> 《Flutter 开发》
**PS:更多精彩内容,请查看 --> 《Flutter 开发》