如何在Flutter中使用JsonSerializable时识别动态键? [英] How to recognize dynamic keys while using JsonSerializable in Flutter?

查看:50
本文介绍了如何在Flutter中使用JsonSerializable时识别动态键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们一直假设,直到最近,我才得到下面的 Json响应

Let's assume, until recently, I was getting the bellow Json response

{
   "AA": {
         "product": {
            "id":"111",
            "type":"C"
         }
    }
}

而且,我编写了下面的代码,将 model.dart 从json转换为对象

And, I have written the bellow, model.dart to convert from json to object

model.dart

@JsonSerializable()
class ResponseGate {
  @JsonKey(name: "AA")
  AA aa;

  ResponseGate({this.aa});
  factory ResponseGate.fromJson(Map<String, dynamic> json) =>
      _$ResponseGateFromJson(json);
}

@JsonSerializable()
class AA {
  @JsonKey(name: "product")
  Product product;

  AA(this.product);
  factory AA.fromJson(Map<String, dynamic> json) => _$AAFromJson(json);
}

@JsonSerializable()
class Product {
  @JsonKey(name: "id")
  var id;
  @JsonKey(name: "type")
  var type;

  Product(this.id, this.type);
  factory Product.fromJson(Map<String, dynamic> json) =>
      _$ProductFromJson(json);
}

然后,响应变得像下面这样.仅添加了另一个对象.

And, then the response becomes as bellow. Just another object is added.

{
   "AA": {
       "product": {
           "id":"111",
           "type":"C"
       }
   },
   "BB": {
       "product": {
           "id":"222",
           "type":"d"
       }
   }
}

键(AA和BB)只是不同,但是它们的子键是相同的.为了使我的代码能够识别所有键并使用 model.dart 从json转换为模型,我该怎么做?

Just the keys (AA & BB) are different but their children are keys are the same. What do I have to do to enable my code to recognize all the keys and use the model.dart to convert from json to model?

if (thirdResponse.statusCode == 200) {
  var map = json.decode(thirdResponse.body) as Map<String, dynamic>;
  ResponseGate responseGate = new ResponseGate.fromJson(map);
else {
  throw Exception('Failed to load post: ${thirdResponse.statusCode}');
}

推荐答案

我看到您已经解码了地图,所以这是代码,我在飞镖.

I saw you have the decoded map, so here is the code, I tested in dartpad.

  1. 迭代 Map

  var data ={
   "AA": {
       "product": {
           "id":"111",
           "type":"C"
       }
   },
   "BB": {
       "product": {
           "id":"222",
           "type":"d"
       }
   }
  };

  var list = new List();
  data.forEach((key, value){
    list.add(value);
  });

  print(list);

  1. 转换为产品


for(var productInfo in list)
{
  Product product = new Product.fromJson(productInfo);
  // add product to your List...
}

或者您可以将两者结合在一起,也可以获取密钥.

Or you could combine both, and get the key also.

  data.forEach((key, value){
     Product product = new Product.fromJson(value);
     // add product to your List...
     // or make a Map to store (key, product)
  });

但是还有另一种方法,将api修改为数组,并在json响应上更有意义.

But there is an alternative way, modify api to array, and make more sense on json response.

[
   {
       "productName": "AA"
       "id":"222",
       "type":"d"
   }
]

开个玩笑,说说最后的办法,因为您没有过多谈论自己的情况.

Just kidding for final approach because you didn't talk too much about your scenario.

这篇关于如何在Flutter中使用JsonSerializable时识别动态键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆