2

my json response is {"quoteText":"You\'re not obligated to win. You\'re obligated to keep trying to do the best you can every day.", "quoteAuthor":"Marian Edelman"} and I am trying to decode by Map<String, dynamic> quoteData = jsonDecode(response.body); but getting this exception

[ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception:
    E/flutter ( 5814): FormatException: Unrecognized string escape (at character 20)
    E/flutter ( 5814): {"quoteText":"You\'re not obligated to win. You\'re obligated to keep tryin...
    E/flutter ( 5814):                    ^
    E/flutter ( 5814): 
    E/flutter ( 5814): #0      _ChunkedJsonParser.fail (dart:convert/runtime/libconvert_patch.dart:1358:5)

Tried by json.decode() and by doing flutter-remove-escape-sequence-in-dart but no luck. Any workaround?

dhar619
  • 55
  • 2
  • 5

1 Answers1

4

That isn't valid json. Single quotes should not be escaped. Either you should get the source to fix it, or you can try to fix the string yourself by wholesale replacing any occurrence of \' with '.

String fixed = badString.replaceAll(r"\'", "'");
json.decode(fixed);
Richard Heap
  • 48,344
  • 9
  • 130
  • 112
  • Thanks! It worked! I am using a third party API service to which I cannot make any changes. This thing worked for me `jsonDecode(response.body.replaceAll(r"\'", "'"));` – dhar619 Mar 11 '19 at 08:21