0

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.

1
  • u can define keys as dynamic or Object Commented Nov 21, 2020 at 5:23

1 Answer 1

1

when you decode a json with jsonDecode, you'll end up having exact types, a key value pair "key":2 when decoded key.runTimeType will be String, value.runTimeType will be int, no matter of how deep they are nested, so you can get assign them like var k=key as String and var v=value as int nested Map and List are supported

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for you answer. I will try your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.