【问题标题】:Dart - Parse Nested Json ArraysDart - 解析嵌套的 Json 数组
【发布时间】:2019-06-08 12:08:57
【问题描述】:

尝试使用 dart 的工厂概念转换嵌套 JSON 数据后出现错误。 这里我有两个类来处理 json 数据,但仍然得到这个错误:

发生了异常。 _TypeError(“FormatException”类型不是“Map”类型的子类型)

代码如下:


    class BodyResponse {
      final Map<String, dynamic> message;
      BodyResponse({
        this.message
      });
      factory BodyResponse.fromJson(Map<String, dynamic> json) {
        return BodyResponse(message: json['message']);
      }
    }

    class ErrorResponse {
      final BodyResponse body;
      final int status;
      final String contentType;
      final bool success;

      ErrorResponse({
        this.body, this.status, this.contentType, this.success
      });

      factory ErrorResponse.fromJson(Map<String, dynamic> json) {
        return ErrorResponse(
          body: BodyResponse.fromJson(json['body']),
          status: json['status'],
          contentType: json['content_type'],
          success: json['success']
        );
      }
    }

    ErrorResponse errors = ErrorResponse.fromJson("""
      {
        "body": {
          "message": "Some one has already taken this username(fooBar), please try again with a new username."
        },
        "status": 500,
        "content_type": "application\/json",
        "success": false
      }
    """);

    print(errors);

这里可能出了什么问题?

【问题讨论】:

  • 您作为参数传递给ErrorResponse errors = ErrorResponse.fromJson(...) 的值似乎是一个字符串,而函数期望它是一个映射 (Map&lt;String, dynamic&gt; json)。
  • @George 但仍然没有办法传递 JSON!

标签: flutter dart


【解决方案1】:

在此处修改了您的大部分代码。希望这是您努力实现的目标。

import 'dart:convert';

class BodyResponse {
  final String message;
  BodyResponse({this.message});

  BodyResponse.fromJson(Map<String, dynamic> json):
    message = json['message'];

  factory BodyResponse.fromString(String encodedJson) {
    return BodyResponse.fromJson(json.decode(encodedJson));
  }

  Map<String, dynamic> toJson() => {
    "message": message,
  };

  String toString() => json.encode(this.toJson());

}

class ErrorResponse {
  final BodyResponse body;
  final int status;
  final String contentType;
  final bool success;

  ErrorResponse({this.body, this.status, this.contentType, this.success});

  ErrorResponse.fromJson(Map<String, dynamic> json):
    body = BodyResponse.fromJson(json['body']),
    status = json['status'],
    contentType = json['content_type'],
    success = json['success'];

  factory ErrorResponse.fromString(String encodedJson) {
    return ErrorResponse.fromJson(json.decode(encodedJson));
  }

  Map<String, dynamic> toJson() => {
    "body": body.toJson(),
    "status": status,
    "contentType": contentType,
    "success": success,
  };

  String toString() => json.encode(this.toJson());

}

void main() {
  ErrorResponse errors = ErrorResponse.fromString("""
      {
        "body": {
          "message": "Some one has already taken this username(fooBar), please try again with a new username."
        },
        "status": 500,
        "content_type": "application\/json",
        "success": false
      }
    """);
  print(errors);
}

如果这有帮助,请告诉我。

【讨论】:

  • 当我在 dart 环境中运行时,您的解决方案工作得很好。但是,当我再次尝试在 catch 块中使用它时,它似乎中断了 :( 这是一个屏幕截图:i.stack.imgur.com/WnOUe.png
  • 我已经尝试过ErrorResponse.fromJsonErrorResponse.fromString 方法
  • 另外,JSON 不知何故在字符串末尾多了一个{i.stack.imgur.com/rcp9G.png
  • @rakibtg 您正试图从本地 Dart 异常 (e) 获取服务器错误响应。我相信你应该使用与fromString 相同的response.body 方法。
  • 感谢乔治的宝贵时间。根据我的 JS 知识,我正在尝试使用这个 try...catch 块:i.imgur.com/07BUPdg.png 你认为根据 dart 可能吗? :)
猜你喜欢
  • 1970-01-01
  • 2021-02-09
  • 1970-01-01
  • 2023-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多