arrays - 如何在 flutter 中解析复杂的 json

标签 arrays json list dart flutter

我无法从复杂的 json 中获取数据,下面是请求中的 json。

{
   "results":{
      "TotalRecordCount":"1",
      "Records":[
         {
            "code":"PCK_34333338365C93E2D50DB9C",
            "address":"1 AV KHEIREDDINE PACHA Imm Pacha centre BLOC B tunis Tunis 1000",
            "contact_phone":"99608258"
         }
      ],
      "Result":"OK"
   }
}

下面是我制作的模型。

import 'dart:convert';

class Pickup {
  String status;
  List message;
  //Map<String ,dynamic> results;
  Results results;
  Pickup(
    {this.status,
     this.message,
     this.results,
  });
  factory Pickup.fromJson(Map<String, dynamic> json) {
    return Pickup(
             status: json["status"] as String,
             results: Results.fromJson(json["results"]),

           );
  }
}

class Results {
  String TotalRecordCount;
  records Records;

  Results({this.TotalRecordCount,this.Records});

  factory Results.fromJson(Map<String, dynamic> json) {
    return Results(
    TotalRecordCount: json["TotalRecordCount"],
    Records:records.fromJson(json["Records"]),

    );
  }
}

class records{
  String code;
  String address;
  String contact_phone;

  records({
    this.code,
    this.address,
    this.contact_phone
  });

  factory records.fromJson(Map<String, dynamic> json) {
    return records(
      code: json["code"],
      address: json["address"],
      contact_phone: json["contact_phone"],
    );
  }
}

现在我正在尝试解析记录以获取代码或地址并打印出来。

if (response.statusCode == 200) {
  print(response.body);
  final responseJson = json.decode(response.body);
  var da=Pickup.fromJson(responseJson);
  Results dat=da.results;
  records data=dat.Records;
  print(data.address);
}

response.body 工作正常,但是当我尝试解析结果或记录时,我得到了 'List' is not a subtype of type 'Map' 错误

最佳答案

我一定会推荐你这个网站json to dart App Quicktype只是不要忘记在右侧选择 Dart。

你只要把你的 json 放在 la left 中,就会给你这样的东西:

// To parse this JSON data, do
//
//     final pickUp = pickUpFromJson(jsonString);

import 'dart:convert';

PickUp pickUpFromJson(String str) => PickUp.fromJson(json.decode(str));

String pickUpToJson(PickUp data) => json.encode(data.toJson());

class PickUp {
    Results results;

    PickUp({
        this.results,
    });

    factory PickUp.fromJson(Map<String, dynamic> json) => new PickUp(
        results: Results.fromJson(json["results"]),
    );

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

class Results {
    String totalRecordCount;
    List<Record> records;
    String result;

    Results({
        this.totalRecordCount,
        this.records,
        this.result,
    });

    factory Results.fromJson(Map<String, dynamic> json) => new Results(
        totalRecordCount: json["TotalRecordCount"],
        records: new List<Record>.from(json["Records"].map((x) => Record.fromJson(x))),
        result: json["Result"],
    );

    Map<String, dynamic> toJson() => {
        "TotalRecordCount": totalRecordCount,
        "Records": new List<dynamic>.from(records.map((x) => x.toJson())),
        "Result": result,
    };
}

class Record {
    String code;
    String address;
    String contactPhone;

    Record({
        this.code,
        this.address,
        this.contactPhone,
    });

    factory Record.fromJson(Map<String, dynamic> json) => new Record(
        code: json["code"],
        address: json["address"],
        contactPhone: json["contact_phone"],
    );

    Map<String, dynamic> toJson() => {
        "code": code,
        "address": address,
        "contact_phone": contactPhone,
    };
}

一开始它会告诉你如何使用它。

// To parse this JSON data, do
//
//     final pickUp = pickUpFromJson(jsonString);

所以当你在代码中调用它时,它会是这样的。

  Future<Pickup> getPickup() async {
    var response = await http.get(url);
    return pickUpFromJson(response.body);
  }

例如,此代码可以调用 FutureBuilder 或您将代码设置为等待 future 的任何地方。

关于arrays - 如何在 flutter 中解析复杂的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55710579/

相关文章:

html - 将 json 对象从一个 HTML 页面传递到另一个 HTML 页面

javascript - 使用 AJAX/JSON 返回整个文件

python - 使用 Python 从列表和字典构建数组

list - 获取 F# 中两个整数列表之间的匹配数

python - If 语句 - 这是一个字符串吗?

c - C中递归函数枚举并返回二维数组n选k的所有组合

arrays - 含糊不清的新声明'unsigned char *

javascript - 在 PHP 数组中调用 JavaScript 函数

SQL JSON - 从数组中获取特定值

javascript - 使用 D3 在 JSON 中嵌套数组