How to recognize dynamic keys while using JsonSerializable in Flutter?

空扰寡人 提交于 2021-01-28 05:30:28

问题


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

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

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"
       }
   }
}

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}');
}

回答1:


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

  1. Iterate 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. Transform To Product

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)
  });

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.




回答2:


don't use models try to put your response in a map then for example

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

if you want "BB" "product" "id" it will be somthing like that

map['BB']['product']['id']


来源:https://stackoverflow.com/questions/57593624/how-to-recognize-dynamic-keys-while-using-jsonserializable-in-flutter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!