有一个发布请求示例:
POST /api HTTP/1.1
Host: checkout.test.paycom.com
X-Auth: 100fe486b33784292111b7dc
Cache-Control: no-cache
{
"id": 123,
"method": "cards.create",
"params": {
"card": { "number": "4444444444444444", "expire": "0420"},
"amount": 350000,
"save": true
}
}
功能:
class Service {
static String url = 'checkout.test.paycom.com';
static Map<String, dynamic> headers = {
'Host': 'checkout.test.paycom.com',
'X-Auth': '3423343243434',
'Cache-Control': 'no-cache'
};
Future createCard(
String id, String cardNumber, String expire, int amount) async {
try {
Map<String, dynamic> body = {
'id': id,
'method': 'cards.create',
'params': {
'card': {
'number': cardNumber,
'expire': expire,
},
'amount': amount,
},
};
final response = await http.post(url, body: body, headers: headers);
if (response.statusCode == 200) {
final result = jsonDecode(response.body);
List<CardModel> data = result['params']['card'];
print(data);
}
} catch (e) {
print(e.toString());
}
return null;
}
}
factory CardModel.fromJson(Map<String, dynamic> json) {
return CardModel(
id: json['id'],
cardNumber: json['number'],
expire: json['expire'],
amount: int.parse(json['amount']),
);
}
给出错误类型“_InternalLinkedHashMap<String, Object>”不是类型转换中“String”类型的子类型。我究竟做错了什么?
方服务器的回答不正确(为了不手写,有:网站,VSCode插件,AS插件,cogen):
如何从json解析数组
如何序列化json