Home > database >  Dart: How to parse Map variable with various special characters like $, {}, " and ' inside
Dart: How to parse Map variable with various special characters like $, {}, " and ' inside

Time:06-13

I have map like this: {"a": "2022-01-26T17:10:51", "b": "{$WWWWWW}", "c":"$$$.id=='TEXT'"} As You see, inside code is a lot of "", $, '' {}. How I can parse it? I could not save this string as map, there is some my tries:


  Map m = {"a": "2022-01-26T17:10:51", "b": "{$WWWWWW}", "c":"$$$.id=='TEXT'"};
  String s1 = r'{"a": "2022-01-26T17:10:51", "b": "{$WWWWWW}", "c":"$$$.id=='TEXT'"}';
  String s2 = '{"a": "2022-01-26T17:10:51", "b": "{$WWWWWW}", "c":"$$$.id=='TEXT'"}';
  String s3 = b'{"a": "2022-01-26T17:10:51", "b": "{$WWWWWW}", "c":"$$$.id=='TEXT'"}';

But none of these line can not pass compilation.

CodePudding user response:

When string started with r and is surrounded by ''' every special characters inside is ignored:

import 'dart:convert';

void main() {
  String s = r'''{"a": "2022-01-26T17:10:51", "b": "{$WWWWWW}", "c":"$$$.id=='TEXT'"}''';
  Map r = jsonDecode(s);
  
  for(var i in r.keys)
  {
    print(r[i]);
  }
}

output: enter image description here

  • Related