3
votes

Below code snippet throws Format Exception in Flutter json decode, Any suggestion please?

import 'dart:async' show Future;
import 'dart:convert';

Future<String> readBooks() async {
  String jsonString = '{"description":"As part of the "You Don’t Know JS" series","quantity":55}';

  Map<String, dynamic> books = json.decode(jsonString);
  return jsonString;
}

Format Exception:

2018-10-23 20:40:30.680 18115-18142/addon.com.bookshop E/flutter: [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception: FormatException: Unexpected character (at character 26) {"name":"As part of the "You Don’t Know JS" series","Age":55} ^

#0      _ChunkedJsonParser.fail (dart:convert/runtime/libconvert_patch.dart:1358:5)
#1      _ChunkedJsonParser.parse (dart:convert/runtime/libconvert_patch.dart:919:48)
#2      _parseJson (dart:convert/runtime/libconvert_patch.dart:29:10)
#3      JsonDecoder.convert (dart:convert/json.dart:540:36)
#4      JsonCodec.decode (dart:convert/json.dart:167:41)
#5      _MyHomePageState.readBooks (file:///E:/Me/TimePass/flutter/book_shop/lib/main.dart:58:39)
<asynchronous suspension>
#6      _MyHomePageState.build.<anonymous closure> (file:///E:/Me/TimePass/flutter/book_shop/lib/main.dart:48:39)
<asynchronous suspension>
#7      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:503:14)
#8      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:558:30)
#9      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
#10     TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
#11     TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7)
#12     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9)
#13     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
#14     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
#15     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:143:19)
#16     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
#17     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
#18     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue

(package:flutter/src/gestures/binding.dart:64:7) #19 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7) #20 _invoke1 (dart:ui/hooks.dart:142:13) #21 _dispatchPointerDataPacket (dart:ui/hooks.dart:99:5)

1

1 Answers

4
votes

That's just invalid JSON.
The inner quotes need to be escaped as \" to make your JSON valid.