Dart很棒,就像编程一样。 IDE也很棒。 我遇到了Dart的问题(对我而言) JSON.parse函数。问题在我看来是一个字符串 是一个整数,它在Map中创建一个整数。这可能是不可取的 因为可能存在字符串字段包含数字的情况。 例如。一个只包含街道号码的地址。或者,也许我是 只是做错了。
暴露可能问题的代码行是:
String sId = mAcctData['I_Id'];
我认为这有一个合理的解决方案,如果是的话,我愿意 感谢被告知
httpSubmitAccountNrLoadEnd(HttpRequest httpReq) {
String sResponse = null;
if (httpReq.status != 200) {
print('On Http Submit Account Nr. There was an error : status = ${httpReq.status}');
} else {
print("On Return from Http Submit Account Nr. no error - status = ${httpReq.status}");
sResponse = httpReq.responseText;
}
print("Response From Submit Account Nr. = ${sResponse}"); // print the received raw JSON text
Map mAcctData = JSON.parse(sResponse);
print ("Json data parsed to map");
print ("Map 'S_Custname' = ${mAcctData['S_Custname']}");
String sCustName = mAcctData['S_Custname'];
print ("About to extract sId from Map data");
String sId = mAcctData['I_Id'];
print ("sId has been extracted from Map data");
String sAcctNr = mAcctData['I_AcctNr'];
String sAcctBal = mAcctData['I_AcctBal'];
以下是控制台输出。 显示我遇到问题的一行是:
Exception: type 'int' is not a subtype of type 'String' of 'sId'.
About to send http message On Return from Http Submit Account Nr. no error - status = 200 Response From Submit Account Nr. = {"S_Custname":"James Bond","I_Id":1,"I_Acctnr":123456789,"I_AcctBal":63727272,"I_AvailBal":0} Json data parsed to map Map 'S_Custname' = James BondAbout to extract sId from Map data
Exception: type 'int' is not a subtype of type 'String' of 'sId'.
Stack Trace: #0 httpSubmitAccountNrLoadEnd (http://127.0.0.1:3030/C:/Users/Brian/dart/transact01/transact01.dart:75:31)
1 submitAccountNr.submitAccountNr. (http://127.0.0.1:3030/C:/Users/Brian/dart/transact01/transact01.dart:33:59)
Exception: type 'int' is not a subtype of type 'String' of 'sId'. Stack Trace: #0 httpSubmitAccountNrLoadEnd (http://127.0.0.1:3030/C:/Users/Brian/dart/transact01/transact01.dart:75:31)
1 submitAccountNr.submitAccountNr. (http://127.0.0.1:3030/C:/Users/Brian/dart/transact01/transact01.dart:33:59)
问题:
答案 0 :(得分:1)
我在sResponse
mAcctData['I_Id']
中看到的内容实际上是int
:
{
"S_Custname": "James Bond",
"I_Id": 1,
"I_Acctnr": 123456789,
"I_AcctBal": 63727272,
"I_AvailBal": 0
}
因此,如果您希望获得String
mAcctData['I_Id']
,可以使用int.toString():
String sId = mAcctData['I_Id'].toString();
答案 1 :(得分:0)
如果您希望它们像这样,只需将整数转换为字符串:
{
"S_Custname": "James Bond",
"I_Id": "1",
"I_Acctnr": "123456789",
"I_AcctBal": "63727272",
"I_AvailBal": "0"
}
现在它应该可以正常工作。或者,使用.toString()
将整数转换为字符串。