flutter 如果所有键都是唯一的,如何用json获取项目?

l7mqbcuq  于 2023-02-09  发布在  Flutter
关注(0)|答案(1)|浏览(115)

我把Json上传到一个条件服务器上,我通过Dio访问这个服务器:

Future getDataDio() async {
    try {
      final Dio dio = Dio();

      final response = await dio
          .get('https://run.mocky.io/v3/26681b8c-6581-4b8b-8fbe-3da2dc7bb785');
      return HystoryOperations.fromJson(response.data);
    } on DioError catch (e) {
      print('error log: ${e.error}');
    }
  }

Json示例我指的是:

{
    "transaction_556505":{
        "date" : "14.01.2022 г.",
        "time" : "00:52",
        "sum" : 351.05,
        "id_order" : 556505,
        "status_order" : "paid",
        "type_order" : "payment_in"
    }, 
    "transaction_556329":{
        "date" : "14.01.2022 г.",
        "time" : "00:59",
        "sum" : 1222.96,
        "id_order" : 556329,
        "status_order" : "payment_not_completed",
        "type_order" : "payment_in"
    }, 
    "transaction_555111":{
        "date" : "13.01.2022 г.",
        "time" : "15:11",
        "sum" : 512.71,
        "id_order" : 555111,
        "status_order" : "in_processing",
        "type_order" : "payment_in"
    }
}

Json序列化已经写好了,其中出现了一个异常,原因是Json请求中的所有键都是唯一的:

@JsonSerializable(explicitToJson: true)
class HystoryOperations {
  final Map<String, dynamic> transaction;

  HystoryOperations({required this.transaction});

  factory HystoryOperations.fromJson(Map<String, dynamic> json) =>
      _$HystoryOperationsFromJson(json);
  Map<String, dynamic> toJson() => _$HystoryOperationsToJson(this);
}

// GENERATED CODE
HystoryOperations _$HystoryOperationsFromJson(Map<String, dynamic> json) =>
    HystoryOperations(
      transaction: json['transaction'] as Map<String, dynamic>, //Exception has occurred. _CastError (type 'Null' is not a subtype of type 'Map<String, dynamic>' in type cast)
    );

Map<String, dynamic> _$HystoryOperationsToJson(HystoryOperations instance) =>
    <String, dynamic>{
      'transaction': instance.transaction,
    };
xuo3flqw

xuo3flqw1#

这很简单。你必须创建两个模型类。一个用于事务列表,一个用于单个事务。然后你可以很容易地从这种类型的JSON中获取所有的项。
您的第一个模型类应如下所示:

class Transactions{
String transaction = "";
List listOfTransactionNames = [];
List <IndividualTransactions> listOfTransaction = [];

Transactions.empty();

Transactions.fromJson(Map<String,dynamic> json){
listOfTransactionNames.addAll(json.keys);
for(int i = 0; i<json.length;i++){
      listOfTransaction.add(
      IndividualTransactions.fromJson(json[listOfTransactionNames[i]]));
    }
  }
}

你的第二个模型类看起来像这样。

class IndividualTransactions {
String date = "14.01.2022 г.";
String time = "00:52";
double sum = 351.05;
int id_order = 556505;
String status_order = "paid";
String type_order = "payment_in";

IndividualTransactions.fromJson(Map<String, dynamic> json) {
  date = json["date"];
  time = json["time"];
  sum = json["sum"];
  id_order = json["id_order"];
  status_order = json["status_order"];
  type_order = json["type_order"];
  }
}

这里发生的事情是:
我们将所有键添加到名为“listOfTransactionNames“的String列表中,然后使用这些键查找所有单个事务。
然后我们使用第二个类的Json方法添加“listOfTransaction”列表中的所有单个值,这些值是通过“listOfTransactionNames”中的键找到的。

相关问题