Dart:将列表列表的字符串表示形式转换为列表列表 [英] Dart: Convert String representation of List of Lists to List of List
本文介绍了Dart:将列表列表的字符串表示形式转换为列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是与此处相同的问题:
this is the same question as here:
但对于Dart而不是python 。我的目的(与另一个问题一样)是采用这样的字符串:
but for Dart rather than python. My aim (as in the other question) is to take a string like:
String stringRep = '[[123],[122],[411]]';
并将其转换为列表列表。我可以看到我可以使用上面引用的答案中推荐的一种方法来实现这一目标,即:
And convert it to a List of lists. I can see I would be able to achieve this using one of the methods recommended in answer referenced above, namely:
str = "[[0,0,0],[0,0,1],[1,1,0]]"
strs = str.replace('[','').split('],')
lists = [map(int, s.replace(']','').split(',')) for s in strs]
但是想知道Dart中是否有更好的方法,但是却很难在网上找到任何东西吗?
But wondering if there is a better method in Dart but struggling to fnd any online?
推荐答案
您可以使用JSON解码器
You can use the JSON decoder
import 'dart:convert';
...
var lists = JSON.decode('[[123],[122],[411]]');
更新
final regExp = new RegExp(r'(?:\[)?(\[[^\]]*?\](?:,?))(?:\])?');
final input = '[[sometext],[122],[411]]';
final result = regExp.allMatches(input).map((m) => m.group(1))
.map((String item) => item.replaceAll(new RegExp(r'[\[\],]'), ''))
.map((m) => [m])
.toList();
print(result);
这篇关于Dart:将列表列表的字符串表示形式转换为列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文