Home > Blockchain >  Decode complex json in flutter/dart
Decode complex json in flutter/dart

Time:03-15

Could someone help me with decode this json and also access the variables?

{
  "data" : [
     "items": {
                "388488": {
                    "id": 388488,
                    "name": "Galaxy Tab A7 10.4 2020",
                    "qty": 1,
                },
                "388489": {
                    "id": 388489,
                    "name": "Samsung Galaxy Tab A7 10.4 2020",
                    "qty": 1,
                },
                "388490": {
                    "id": 388490,
                    "name": " Apple iPad 10.2",
                    "qty": 1,
                }
            }
       ]
 }

Note : the 388488 , 388489 and 388490 aren't stable and in other items can be different so i cant create a variable and name it something specific in model class.

here is my model class:

class OrderPageModel{
  late final List<OrderPageModelData> orderpagemodeldata;

  OrderPageModel({
   required this.orderpagemodeldata
});

  factory OrderPageModel.fromJson(Map<String , dynamic>Json ){
    var list = Json["data"] as List ;
    List<OrderPageModelData> dataList = list.map((e) => OrderPageModelData.fromJson(e)).toList();
    return OrderPageModel(
      order_id: Json['order_id'],
        orderpagemodeldata: dataList
    );
  }
}

class OrderPageModelData{
  late Items items;
  
  OrderPageModelData({
   required this.items 
});
  factory OrderPageModelData.fromJson(Map<String , dynamic> Json ){
    return OrderPageModelData(
        items: Items.fromJson(Json['items'])
    );
  }
}

class Items{
  late final id;
  late final name;
  late final quantity;

  Items({
   this.id,
   this.name,
   this.quantity,
});
  
  
  factory Items.fromJson(Map<String , dynamic> Json){
    return Items(
      id: Json["id"],
      name: Json["name"],
      quantity: Json["qty"]
    );
  }

}

and here is how i decode it:

if(response.statusCode == 200){
      var decodedResponse = jsonDecode(response.body);
      orderPageModel = OrderPageModel.fromJson(decodedResponse);

      for(var i in orderPageModel.orderpagemodeldata){
        print(i.items.name);

      }
    }

when I run my codes what I get is null it mean the name of item isn't define and idk what is wrong with me codes. Also when I run print(i.items) it will return Instance of Items. Could anyone help me how to fix this?

CodePudding user response:

Your Json['items'] is a JSON object. You cannot directly map it to Items (whose name counterintuitively implies it represents multiple items, which is not the case), but to Map<String, dynamic>. From there, you can iterate through it (using the entries property), and pass each value to Items.fromJson.

Also, your JSON is invalid, as it is.

CodePudding user response:

Try to use this approaches: https://javiercbk.github.io/json_to_dart/ for setup models and Serializing JSON using code generation libraries https://docs.flutter.dev/development/data-and-backend/json for parsing json good tutorial for using this tools is here https://www.youtube.com/watch?v=v5xGLrhzDGE&t=146s

  • Related