How to deserialize JSON with dynamic keys in flutter

Following is the api response I have

{
  "19ca14e7ea6328a42e0eb13d585e4c22":{
    "key":"19ca14e7ea6328a42e0eb13d585e4c22",
    "product_id":36,
    "variation_id":0,
    "variation":[],
    "quantity":1,
    "data_hash":"b5c1d5ca8bae6d4896cf1807cdf763f0",
    "line_tax_data": {
      "subtotal": {
        "12": 8.4
      },
      "total": {
        "12": 8.4
      }
    },
    "line_subtotal":18,
    "line_subtotal_tax":8.4,
    "line_total":14.4,
    "line_tax":8.4,
    "data":{},
    "product_name":"Vneck Tshirt",
    "product_title":"Vneck Tshirt",
    "product_price": "£18",
    "product_image":"https://example.com/wp-content/uploads/2019/06/vneck-tee.jpg"
  }
}

Following is the Deserialized Object I have.

import 'dart:convert';

AddToCartResponseModel addToCartResponseModelFromJson(String str) => AddToCartResponseModel.fromJson(json.decode(str));

String addToCartResponseModelToJson(AddToCartResponseModel data) => json.encode(data.toJson());

class AddToCartResponseModel {
    AddToCartResponseModel({
        this.the19Ca14E7Ea6328A42E0Eb13D585E4C22,
    });

    The19Ca14E7Ea6328A42E0Eb13D585E4C22 the19Ca14E7Ea6328A42E0Eb13D585E4C22;

    factory AddToCartResponseModel.fromJson(Map<String, dynamic> json) => AddToCartResponseModel(
        the19Ca14E7Ea6328A42E0Eb13D585E4C22: The19Ca14E7Ea6328A42E0Eb13D585E4C22.fromJson(json["19ca14e7ea6328a42e0eb13d585e4c22"]),
    );

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

class The19Ca14E7Ea6328A42E0Eb13D585E4C22 {
    The19Ca14E7Ea6328A42E0Eb13D585E4C22({
        this.key,
        this.productId,
        this.variationId,
        this.variation,
        this.quantity,
        this.dataHash,
        this.lineTaxData,
        this.lineSubtotal,
        this.lineSubtotalTax,
        this.lineTotal,
        this.lineTax,
        this.data,
        this.productName,
        this.productTitle,
        this.productPrice,
        this.productImage,
    });

    String key;
    int productId;
    int variationId;
    List<dynamic> variation;
    int quantity;
    String dataHash;
    LineTaxData lineTaxData;
    int lineSubtotal;
    double lineSubtotalTax;
    double lineTotal;
    double lineTax;
    Data data;
    String productName;
    String productTitle;
    String productPrice;
    String productImage;

    factory The19Ca14E7Ea6328A42E0Eb13D585E4C22.fromJson(Map<String, dynamic> json) => The19Ca14E7Ea6328A42E0Eb13D585E4C22(
        key: json["key"],
        productId: json["product_id"],
        variationId: json["variation_id"],
        variation: List<dynamic>.from(json["variation"].map((x) => x)),
        quantity: json["quantity"],
        dataHash: json["data_hash"],
        lineTaxData: LineTaxData.fromJson(json["line_tax_data"]),
        lineSubtotal: json["line_subtotal"],
        lineSubtotalTax: json["line_subtotal_tax"].toDouble(),
        lineTotal: json["line_total"].toDouble(),
        lineTax: json["line_tax"].toDouble(),
        data: Data.fromJson(json["data"]),
        productName: json["product_name"],
        productTitle: json["product_title"],
        productPrice: json["product_price"],
        productImage: json["product_image"],
    );

    Map<String, dynamic> toJson() => {
        "key": key,
        "product_id": productId,
        "variation_id": variationId,
        "variation": List<dynamic>.from(variation.map((x) => x)),
        "quantity": quantity,
        "data_hash": dataHash,
        "line_tax_data": lineTaxData.toJson(),
        "line_subtotal": lineSubtotal,
        "line_subtotal_tax": lineSubtotalTax,
        "line_total": lineTotal,
        "line_tax": lineTax,
        "data": data.toJson(),
        "product_name": productName,
        "product_title": productTitle,
        "product_price": productPrice,
        "product_image": productImage,
    };
}

class Data {
    Data();

    factory Data.fromJson(Map<String, dynamic> json) => Data(
    );

    Map<String, dynamic> toJson() => {
    };
}

class LineTaxData {
    LineTaxData({
        this.subtotal,
        this.total,
    });

    Total subtotal;
    Total total;

    factory LineTaxData.fromJson(Map<String, dynamic> json) => LineTaxData(
        subtotal: Total.fromJson(json["subtotal"]),
        total: Total.fromJson(json["total"]),
    );

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

class Total {
    Total({
        this.the12,
    });

    double the12;

    factory Total.fromJson(Map<String, dynamic> json) => Total(
        the12: json["12"].toDouble(),
    );

    Map<String, dynamic> toJson() => {
        "12": the12,
    };
}

How to deserialize this properly. As the keys are dynamic here.



Read more here: https://stackoverflow.com/questions/64939697/how-to-deserialize-json-with-dynamic-keys-in-flutter

Content Attribution

This content was originally published by Sachin Soma at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: