飛鏢是偉大的,這就像編程。 IDE也很棒。 我遇到了在我看來是一個問題(對我來說)Dart JSON.parse函數。在我看來,問題是如果一個字符串 是一個整數,它會在Map中創建一個整數。這可能並不理想 ,因爲可能會出現字符串字段包含數字的情況。 例如。一個只包含街道號碼的地址。或者,也許我是 只是做錯了什麼。JSON.parse映射想要的字符串得到整數
的代碼暴露可能出現的問題的行是:
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)
問題:
- 有沒有解決辦法? (例如,強制JSON.parse創建字符串)
- 有沒有更好的方法來處理這個問題? (如:直接從JSON字符串中提取數據)
好吧,是我不好。我以爲它是從它創建一個整數,因爲它是數字數據,而不是它是字符串表示中的數字字段。 –