【问题标题】:JSON.parse Map want String get integerJSON.parse Map 想要 String 获取整数
【发布时间】:2012-12-25 16:24:29
【问题描述】:

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'.

即将发送http消息 从 Http 返回时提交帐户编号。没有错误 - 状态 = 200 来自提交帐户编号的响应。 = {"S_Custname":"詹姆斯邦德","I_Id":1,"I_Acctnr":123456789,"I_AcctBal":63727272,"I_AvailBal":0} Json 数据解析为 map 地图“S_Custname”=詹姆斯邦德

关于从地图数据中提取sId

例外:“int”类型不是“sId”的“String”类型的子类型。

堆栈跟踪:#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)

例外:“int”类型不是“sId”的“String”类型的子类型。 堆栈跟踪:#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)

问题:

  1. 有解决办法吗? (例如,强制 JSON.parse 创建字符串)
  2. 有没有更好的方法来处理这个问题? (例如,直接从 JSON 字符串中提取数据)

【问题讨论】:

    标签: json dart


    【解决方案1】:

    从我在您的sResponse 中看到的mAcctData['I_Id'] 确实是int

    {
      "S_Custname": "James Bond",
      "I_Id": 1,
      "I_Acctnr": 123456789,
      "I_AcctBal": 63727272,
      "I_AvailBal": 0
    }
    

    因此,如果您想为mAcctData['I_Id'] 获取String,您可以使用int.toString()

    String sId = mAcctData['I_Id'].toString();
    

    【讨论】:

    • 好吧,我的错。我认为它是从它创建一个整数,因为它是数字数据,而不是它是来自字符串表示形式的数字字段。
    【解决方案2】:

    如果您希望整数变成这样,只需将它们转换为字符串即可:

    {
        "S_Custname": "James Bond",
        "I_Id": "1",
        "I_Acctnr": "123456789",
        "I_AcctBal": "63727272",
        "I_AvailBal": "0"
    }
    

    现在它应该可以正常工作了。或者,使用.toString() 将整数转换为字符串。

    【讨论】:

      猜你喜欢
      • 2015-10-11
      • 2022-11-02
      • 1970-01-01
      • 2023-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-25
      • 1970-01-01
      相关资源
      最近更新 更多