Home > Mobile >  add escape character to json object flutter
add escape character to json object flutter

Time:11-22

How to convert json object like this

{"firstname": "John", "lastname": "Doe"}

to this

"{\"firstname\": \"John\", \"lastname\": \"Doe\"}"

thank you.

CodePudding user response:

Try this:

final data = {"name": "John"};
print(jsonEncode(data).replaceAll('"', '\\"'));

CodePudding user response:

Let's try

void main() {
  var data = {"firstname": "John", "lastname": "Doe"};
  Map<String, dynamic> newMap = {};
  data.keys.toList().forEach((key) {
    newMap.addAll({"\\${key}\\": "\\${data[key]}\\"});
  });
  print(newMap);//{\firstname\: \John\, \lastname\: \Doe\}
}
  • Related