【问题标题】:Parsing double values from JSON in Flutter and Dart在 Flutter 和 Dart 中从 JSON 解析双精度值
【发布时间】:2019-06-14 02:11:51
【问题描述】:

我在 Flutter 中尝试从 JSON 获取双精度值时遇到问题。

class MapPoint {
  String title;
  double lat;
  double lng;

  MapPoint({this.title, this.lat, this.lng});

  factory MapPoint.fromJson(Map<String, dynamic> json) {
    return MapPoint(
        title: json["title"] as String,
        lat: json["lat"] as double,
        lng: json["lng"] as double
    );
  }
}

由于某种原因,我得到了错误

Dart 错误:未处理的异常:类型“double”不是 输入“字符串”

我向用户 double.parse(json["lng"]) 尝试了一些,但得到了同样的错误。
同时,这种从 JSON 获取数据的方式也适用于其他类型。

这是 JSON 示例

{ 
   point: {
     title: "Point title",
     lat: 42.123456,
     lng: 32.26567
  }
}

【问题讨论】:

  • 什么 JSON 会导致这个错误?如果您的值用引号括起来,它们将不会被解释为双精度值。
  • JSON 很好,因为我可以从中获取所有其他值(sting 和 int)
  • 要么 JSON 不适合您的代码,要么您的代码不适合您的 JSON。如果不提供导致错误的 JSON,您也可以删除问题,因为它没有提供诊断问题所需的信息。
  • 我添加了 JSON 的示例。可能不寻常的一件事是双精度值不会以字符串形式呈现。
  • 那不是有效的 JSON。在 JSON 中,必须引用键,并且在 "Point title" 之后缺少 ,。双精度值在 JSON 中永远不会表示为 String

标签: json dart flutter double


【解决方案1】:

Dart JSON 解析器转换 json 中的属性,显然它足够聪明,可以吐出一个 double 类型。

someVariable as double 期望左侧有一个字符串。

可能发生的事情是您试图将双精度转换为双精度。

我会尝试这样的事情:

lat: json["lat"].toDouble(),

这将涵盖 JSON 中的数据类似于“5”的情况。在这种情况下,dart json 转换器会将类型转换为 int,如果您总是期待双精度,它会破坏您的代码。

【讨论】:

  • 纬度:double.parse(json["lat"])
  • 如果您不确定类型是 int 还是 double;最好将其声明为num
【解决方案2】:

我遇到了同样的问题,我认为是这样的:

class MapPoint {
  String title;
  double lat;
  double lng;

  MapPoint({this.title, this.lat, this.lng});

  factory MapPoint.fromJson(Map<String, dynamic> json) {
    return MapPoint(
        title: json["title"] as String,
        lat: json["lat"] is int ? (json['lat'] as int).toDouble() : json['lat'],
        lng: json["lng"] is int ? (json['lng'] as int).toDouble() : json['lng']
    );
  }
}

【讨论】:

    【解决方案3】:

    我无法复制

    void main() {
      final json = {
        "point": {"title": "Point title", "lat": 42.123456, "lng": 32.26567}
      };
      final p = MapPoint.fromJson(json);
      print(p);
    }
    
    class MapPoint {
      String title;
      double lat;
      double lng;
    
      MapPoint({this.title, this.lat, this.lng});
    
      factory MapPoint.fromJson(Map<String, dynamic> json) {
        return MapPoint(
            title: json["title"] as String,
            lat: json["lat"] as double,
            lng: json["lng"] as double);
      }
    }
    

    【讨论】:

      【解决方案4】:

      我可以这样解决这个问题

      class MapPoint {
        String title;
        double lat;
        double lng;
      
        MapPoint({this.title, this.lat, this.lng});
      
        factory MapPoint.fromJson(Map<String, dynamic> json) {
          return MapPoint(
              title: json["title"] as String,
              lat: json["lat"] *1.0,
              lng: json["lng"] *1.0
          );
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-07-01
        • 2016-03-03
        • 2020-11-25
        • 1970-01-01
        • 2018-09-11
        • 2012-01-10
        • 1970-01-01
        • 2021-07-10
        • 2022-01-09
        相关资源
        最近更新 更多