简体   繁体   English

颤动如何将列表字符串转换为列表

[英]flutter how to convert list string to list

I am trying to change a list string to list, there is the string:我正在尝试将列表字符串更改为列表,有字符串:

var liststr = "[['one', 'two', 'three', 'four'], ['one\'s next', 'two\'s next', 'three\'s next', 'four\'s next']]";

I tried json.decode, but report error because single quotes, I don't know other ways to solve this problem.我试过json.decode,但是因为单引号报错,不知道有其他方法可以解决这个问题。

so I want to ask how to solve it in dart, thank you~所以想问一下dart怎么解决,谢谢~

Check Out following way to convert string list into list of string.查看以下将字符串列表转换为字符串列表的方法。

1) This joints all the elements in one list. 1) 这将所有元素连接到一个列表中。

var list =
    "[['one', 'two', 'three', 'four'], ['one\'s next', 'two\'s next', 'three\'s next', 'four\'s next']]";
final regExp =
    new RegExp(r"(\w+\\?\'?\w*\s?\w+)"); 
var result = regExp
    .allMatches(list)
    .map((m) => m.group(1))
    .map((String item) => item.replaceAll(new RegExp(r'[\[\],]'), ''))
    .map((m) => m)
    .toList();
print(result);

Output: [one, two, three, four, one, s, next, two, s, next, three, s, next, four, s, next]输出:[一,二,三,四,一,s,下一个,二,s,下一个,三,s,下一个,四,s,下一个]

2) If You want it list item wise then follow below code. 2)如果您希望它明智地列出项目,请按照以下代码进行操作。

   var list =
        "[['one', 'two', 'three', 'four'], ['one\'s next', 'two\'s next', 'three\'s next', 'four\'s next']]";
    final regExp = new RegExp(r'(?:\[)?(\[[^\]]*?\](?:,?))(?:\])?');
    final regExp2 = new RegExp(r"(\w+\\?\'?\w*\s?\w+)");
    final result = regExp
        .allMatches(list)
        .map((m) => m.group(1))
        .map((String item) => item.replaceAll(new RegExp(r'[\[\],]'), ''))
        .map((m) => [m])
        .toList();
    final result2 = List();
    for (int i = 0; i < result.length; i++) {
      result2.add(regExp2
          .allMatches(result[i].toString())
          .map((m) => m.group(1))
          .map((m) => m)
          .toList());
    }

    print(result2[0]);
    print(result2[0][0]);

output: [one, two, three, four]输出:[一、二、三、四]

one

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM