下面的代码片段在Flutter json解码中引发Format Exception,请问有什么建议吗?
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;
}
格式异常:
2018-10-23 20:40:30.680 18115-18142 / addon.com.bookshop E / flutter: [错误:flutter / shell / common / shell.cc(181)] Dart错误:未处理 例外: FormatException:意外字符(在字符26处) {“名称”:“作为“您不知道JS”系列的一部分”,“年龄”: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 (包:flutter / src / gestures / binding.dart:48:7) #20 _invoke1(dart:ui / hooks.dart:142:13) #21 _dispatchPointerDataPacket(dart:ui / hooks.dart:99:5)
答案 0 :(得分:2)
那只是无效的JSON。
内部引号需要转为\"
才能使JSON有效。