Flutter :- 如何在 flutter 中解析模型 class 中的 JSON?
Flutter :- How to parse the JSON from the model class in flutter?
我在 flutter
中解析键和值中的 JSON,如下面的代码
Future<LoginBean> login() async {
var body = json.encode({"MOB": "1112223330", "KEY": "123456"});
return http.post(
Uri.encodeFull(
"https://MY_Server/Users/login"),
body: body.toString(),
headers: {'Content-type': 'application/json'}).then((response) {
print("Response Status : $response");
if (response.statusCode == 200) {
Map userMap = jsonDecode(json);
var user = new LoginBean.fromJson(userMap);
debugPrint("here is the response==>>>> $user");
}
});
}
是否有任何其他方法可以像我们在Android
中那样解析模型class中的JSON,请查看以下示例.
Gson gson = new Gson();
MY_LOGIN_BEAN loginBean = gson.fromJson(response, MY_LOGIN_BEAN.class);
我已经参考了这个链接但没有得到解决方案,请查看以下内容:-
这是我的JSON回复
{"status":1,"message":"Login Successfully","data":{"userid":"101","Authorization":"eyJ1c2VyaWQiOiIxMDEiLCJ0b2tlbiI6IjVjYWRkYzUzMWY4YzAifQ==","roles":"2","firstname":"Ravindra","lastname":"kushwaha","fullname":"Ravindra kushwaha","wallet_amount":"845.00","mobile":"1112223330","email":"ravindra.kushwaha@consagous.com","chat_id":"","qrcode_image":"https:\/\/dapplepay.consagous.co.in\/uploads\/coupon_qr\/11C1f7ApJM8r.png","redirect_to_verify":"0","notification_status":"0","country_code":"91","Is_Allowed_Transaction":"1","profile_image":"https:\/\/dapplepay.consagous.co.in\/uploads\/user\/1553600479DapplePay1553600454461.png","fingerprint_status":"0"}}
我试过下面几行代码,请看一下,但我没有成功。
class LoginBean {
int status;
String message;
Data data;
LoginBean({
this.status,
this.message,
this.data,
});
static Map<String, dynamic> toMap(LoginBean loginBean){
var map = Map<String, dynamic>();
map['status'] = loginBean.status;
map['message'] = loginBean.message;
map['data'] = Data.toMap(loginBean);
return map;
}
LoginBean.map(dynamic obj) {
this.status = obj["status"];
this.message = obj["message"];
if (obj['data'] != null) {
this.data = new Data.map(obj['data']);
}
}
factory LoginBean.fromJson(dynamic json) {
print('here we gooo');
return LoginBean(
status: json['status'],
message: json['message'],
data: new Data.fromJson(json['data'])
);
}
}
class Data {
String userid;
String authorization;
String roles;
String firstname;
String lastname;
String fullname;
String walletAmount;
String mobile;
String email;
String chatId;
String qrcodeImage;
String redirectToVerify;
String notificationStatus;
String countryCode;
String isAllowedTransaction;
String profileImage;
String fingerprintStatus;
Data.map(dynamic json) {
this.userid = json['userid'];
this.authorization = json['authorization'];
this.roles = json['roles'];
this.firstname = json['firstname'];
this.lastname = json['lastname'];
this.fullname = json['fullname'];
this.walletAmount = json['walletAmount'];
this.mobile = json['mobile'];
this.email = json['email'];
this.chatId = json['chatId'];
this.qrcodeImage = json['qrcodeImage'];
this.redirectToVerify = json['redirectToVerify'];
this.notificationStatus = json['notificationStatus'];
this.countryCode= json['countryCode'];
this.isAllowedTransaction = json['isAllowedTransaction'];
this.profileImage = json['profileImage'];
this.fingerprintStatus = json['fingerprintStatus'];
}
static Map<String, dynamic> toMap(LoginBean loginBean){
var map = Map<String, dynamic>();
map['userid'] = loginBean.data.userid;
map['authorization'] = loginBean.data.authorization;
map['roles'] = loginBean.data.roles;
map['firstname'] = loginBean.data.firstname;
map['lastname'] = loginBean.data.lastname;
map['fullname'] = loginBean.data.fullname;
map['walletAmount'] = loginBean.data.walletAmount;
map['mobile'] = loginBean.data.mobile;
map['email'] = loginBean.data.email;
map['chatId'] = loginBean.data.chatId;
map['qrcodeImage'] = loginBean.data.qrcodeImage;
map['redirectToVerify'] = loginBean.data.redirectToVerify;
map['notificationStatus'] = loginBean.data.notificationStatus;
map['countryCode'] = loginBean.data.countryCode;
map['isAllowedTransaction'] = loginBean.data.isAllowedTransaction;
map['profileImage'] = loginBean.data.profileImage;
map['fingerprintStatus'] = loginBean.data.fingerprintStatus;
return map;
}
factory Data.fromJson(dynamic json) {
return Data(
userid: json['userid'],
authorization: json['authorization'],
roles: json['roles'],
firstname: json['firstname'],
lastname: json['lastname'],
fullname: json['fullname'],
walletAmount: json['walletAmount'],
mobile: json['mobile'],
email: json['email'],
chatId: json['chatId'],
qrcodeImage: json['qrcodeImage'],
redirectToVerify: json['redirectToVerify'],
notificationStatus: json['notificationStatus'],
countryCode: json['countryCode'],
isAllowedTransaction: json['isAllowedTransaction'],
profileImage: json['profileImage'],
fingerprintStatus: json['fingerprintStatus'],
);
}
Data({
this.userid,
this.authorization,
this.roles,
this.firstname,
this.lastname,
this.fullname,
this.walletAmount,
this.mobile,
this.email,
this.chatId,
this.qrcodeImage,
this.redirectToVerify,
this.notificationStatus,
this.countryCode,
this.isAllowedTransaction,
this.profileImage,
this.fingerprintStatus,
});
}
我在使用模型 class 解析 JSON
时从上面的代码中得到以下异常
2019-04-10 18:59:28.553 11121-11153/democom.first_flutter_app E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
#0 _LoginScreen.login.<anonymous closure> (package:first_flutter_app/onBording/login_screen.dart:139:34)
#1 _rootRunUnary (dart:async/zone.dart:1132:38)
#2 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#3 _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
#4 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
#5 Future._propagateToListeners (dart:async/future_impl.dart:668:32)
#6 Future._complete (dart:async/future_impl.dart:473:7)
#7 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
#8 _AsyncAwaitCompleter.complete (dart:async/runtime/libasync_patch.dart:28:18)
#9 _completeOnAsyncReturn (dart:async/runtime/libasync_patch.dart:294:13)
#10 _withClient (package:http/http.dart)
<asynchronous suspension>
#11 post (package:http/http.dart:70:3)
#12 _LoginScreen.login (package:first_flutter_app/onBording/login_screen.dart:129:12)
<asynchronous suspension>
#13 _LoginScreen.build.<anonymous closure>.<anonymous closure> (package:first_flutter_app/onBording/login_screen.dart:102:25)
#14 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:513:14)
#15 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:568:30)
#16 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:120:24)
#17 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
#18 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7)
#19 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:369:9)
#20 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
#21 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
#22 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:214:19)
#23 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:192:22)
#24 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:149:7)
#25 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7)
#26 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7)
#27 _rootRunUnary (dart:async/zone.dart:1136:13)
#28 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#29 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
#30 _invoke1 (dart:ui/hooks.dart:223:10)
#31 _dispatchPointerDataPacket (dart:ui/hooks.dart:144:5)
你正在解码 json
而你应该解码响应中收到的正文 -
var userMap = jsonDecode(response?.body);
我在 flutter
中解析键和值中的 JSON,如下面的代码 Future<LoginBean> login() async {
var body = json.encode({"MOB": "1112223330", "KEY": "123456"});
return http.post(
Uri.encodeFull(
"https://MY_Server/Users/login"),
body: body.toString(),
headers: {'Content-type': 'application/json'}).then((response) {
print("Response Status : $response");
if (response.statusCode == 200) {
Map userMap = jsonDecode(json);
var user = new LoginBean.fromJson(userMap);
debugPrint("here is the response==>>>> $user");
}
});
}
是否有任何其他方法可以像我们在Android
中那样解析模型class中的JSON,请查看以下示例.
Gson gson = new Gson();
MY_LOGIN_BEAN loginBean = gson.fromJson(response, MY_LOGIN_BEAN.class);
我已经参考了这个链接但没有得到解决方案,请查看以下内容:-
这是我的JSON回复
{"status":1,"message":"Login Successfully","data":{"userid":"101","Authorization":"eyJ1c2VyaWQiOiIxMDEiLCJ0b2tlbiI6IjVjYWRkYzUzMWY4YzAifQ==","roles":"2","firstname":"Ravindra","lastname":"kushwaha","fullname":"Ravindra kushwaha","wallet_amount":"845.00","mobile":"1112223330","email":"ravindra.kushwaha@consagous.com","chat_id":"","qrcode_image":"https:\/\/dapplepay.consagous.co.in\/uploads\/coupon_qr\/11C1f7ApJM8r.png","redirect_to_verify":"0","notification_status":"0","country_code":"91","Is_Allowed_Transaction":"1","profile_image":"https:\/\/dapplepay.consagous.co.in\/uploads\/user\/1553600479DapplePay1553600454461.png","fingerprint_status":"0"}}
我试过下面几行代码,请看一下,但我没有成功。
class LoginBean {
int status;
String message;
Data data;
LoginBean({
this.status,
this.message,
this.data,
});
static Map<String, dynamic> toMap(LoginBean loginBean){
var map = Map<String, dynamic>();
map['status'] = loginBean.status;
map['message'] = loginBean.message;
map['data'] = Data.toMap(loginBean);
return map;
}
LoginBean.map(dynamic obj) {
this.status = obj["status"];
this.message = obj["message"];
if (obj['data'] != null) {
this.data = new Data.map(obj['data']);
}
}
factory LoginBean.fromJson(dynamic json) {
print('here we gooo');
return LoginBean(
status: json['status'],
message: json['message'],
data: new Data.fromJson(json['data'])
);
}
}
class Data {
String userid;
String authorization;
String roles;
String firstname;
String lastname;
String fullname;
String walletAmount;
String mobile;
String email;
String chatId;
String qrcodeImage;
String redirectToVerify;
String notificationStatus;
String countryCode;
String isAllowedTransaction;
String profileImage;
String fingerprintStatus;
Data.map(dynamic json) {
this.userid = json['userid'];
this.authorization = json['authorization'];
this.roles = json['roles'];
this.firstname = json['firstname'];
this.lastname = json['lastname'];
this.fullname = json['fullname'];
this.walletAmount = json['walletAmount'];
this.mobile = json['mobile'];
this.email = json['email'];
this.chatId = json['chatId'];
this.qrcodeImage = json['qrcodeImage'];
this.redirectToVerify = json['redirectToVerify'];
this.notificationStatus = json['notificationStatus'];
this.countryCode= json['countryCode'];
this.isAllowedTransaction = json['isAllowedTransaction'];
this.profileImage = json['profileImage'];
this.fingerprintStatus = json['fingerprintStatus'];
}
static Map<String, dynamic> toMap(LoginBean loginBean){
var map = Map<String, dynamic>();
map['userid'] = loginBean.data.userid;
map['authorization'] = loginBean.data.authorization;
map['roles'] = loginBean.data.roles;
map['firstname'] = loginBean.data.firstname;
map['lastname'] = loginBean.data.lastname;
map['fullname'] = loginBean.data.fullname;
map['walletAmount'] = loginBean.data.walletAmount;
map['mobile'] = loginBean.data.mobile;
map['email'] = loginBean.data.email;
map['chatId'] = loginBean.data.chatId;
map['qrcodeImage'] = loginBean.data.qrcodeImage;
map['redirectToVerify'] = loginBean.data.redirectToVerify;
map['notificationStatus'] = loginBean.data.notificationStatus;
map['countryCode'] = loginBean.data.countryCode;
map['isAllowedTransaction'] = loginBean.data.isAllowedTransaction;
map['profileImage'] = loginBean.data.profileImage;
map['fingerprintStatus'] = loginBean.data.fingerprintStatus;
return map;
}
factory Data.fromJson(dynamic json) {
return Data(
userid: json['userid'],
authorization: json['authorization'],
roles: json['roles'],
firstname: json['firstname'],
lastname: json['lastname'],
fullname: json['fullname'],
walletAmount: json['walletAmount'],
mobile: json['mobile'],
email: json['email'],
chatId: json['chatId'],
qrcodeImage: json['qrcodeImage'],
redirectToVerify: json['redirectToVerify'],
notificationStatus: json['notificationStatus'],
countryCode: json['countryCode'],
isAllowedTransaction: json['isAllowedTransaction'],
profileImage: json['profileImage'],
fingerprintStatus: json['fingerprintStatus'],
);
}
Data({
this.userid,
this.authorization,
this.roles,
this.firstname,
this.lastname,
this.fullname,
this.walletAmount,
this.mobile,
this.email,
this.chatId,
this.qrcodeImage,
this.redirectToVerify,
this.notificationStatus,
this.countryCode,
this.isAllowedTransaction,
this.profileImage,
this.fingerprintStatus,
});
}
我在使用模型 class 解析 JSON
时从上面的代码中得到以下异常2019-04-10 18:59:28.553 11121-11153/democom.first_flutter_app E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
#0 _LoginScreen.login.<anonymous closure> (package:first_flutter_app/onBording/login_screen.dart:139:34)
#1 _rootRunUnary (dart:async/zone.dart:1132:38)
#2 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#3 _FutureListener.handleValue (dart:async/future_impl.dart:126:18)
#4 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:639:45)
#5 Future._propagateToListeners (dart:async/future_impl.dart:668:32)
#6 Future._complete (dart:async/future_impl.dart:473:7)
#7 _SyncCompleter.complete (dart:async/future_impl.dart:51:12)
#8 _AsyncAwaitCompleter.complete (dart:async/runtime/libasync_patch.dart:28:18)
#9 _completeOnAsyncReturn (dart:async/runtime/libasync_patch.dart:294:13)
#10 _withClient (package:http/http.dart)
<asynchronous suspension>
#11 post (package:http/http.dart:70:3)
#12 _LoginScreen.login (package:first_flutter_app/onBording/login_screen.dart:129:12)
<asynchronous suspension>
#13 _LoginScreen.build.<anonymous closure>.<anonymous closure> (package:first_flutter_app/onBording/login_screen.dart:102:25)
#14 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:513:14)
#15 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:568:30)
#16 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:120:24)
#17 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
#18 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7)
#19 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:369:9)
#20 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
#21 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
#22 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:214:19)
#23 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:192:22)
#24 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:149:7)
#25 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7)
#26 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7)
#27 _rootRunUnary (dart:async/zone.dart:1136:13)
#28 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
#29 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
#30 _invoke1 (dart:ui/hooks.dart:223:10)
#31 _dispatchPointerDataPacket (dart:ui/hooks.dart:144:5)
你正在解码 json
而你应该解码响应中收到的正文 -
var userMap = jsonDecode(response?.body);