Home > Mobile >  Advanced json parse in dart
Advanced json parse in dart

Time:09-23

I want to parse this Json. Now i am parsing Load as a class with 2 fields: id, and value. But what i want to is to parse it like Map<int, String>. When i am trying to cast the object, flutter gives me an error. for example:

Map<int, String> map = {
  38 : "#1038 Fort Dodge, IA - Algona, IA",
  39 : "#1039 Louisville, KY - Louisville, KY"
}

it is not the same as this question

{
    "loads": {
      "data": [
        {
          "id": 38,
          "value": "#1038 Fort Dodge, IA - Algona, IA"
        },
        {
          "id": 39,
          "value": "#1039 Louisville, KY - Louisville, KY"
        }
      ]
    },
    "date_ranges": {
      "data": [
        {
          "id": "all_time",
          "value": "All"
        },
        {
          "id": "this_year",
          "value": "This year"
        }
      ]
    }
  }

CodePudding user response:

You can create pojo class to parse your JSON

TestModel testModel = TestModel.fromJson(yourResponse);


 class TestModel {
  Loads loads;
  Loads dateRanges;

  TestModel({this.loads, this.dateRanges});

  TestModel.fromJson(Map<String, dynamic> json) {
    loads = json['loads'] != null ? new Loads.fromJson(json['loads']) : null;
    dateRanges = json['date_ranges'] != null
        ? new Loads.fromJson(json['date_ranges'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.loads != null) {
      data['loads'] = this.loads.toJson();
    }
    if (this.dateRanges != null) {
      data['date_ranges'] = this.dateRanges.toJson();
    }
    return data;
  }
}

class Loads {
  List<Data> data;

  Loads({this.data});

  Loads.fromJson(Map<String, dynamic> json) {
    if (json['data'] != null) {
      data = new List<Data>();
      json['data'].forEach((v) {
        data.add(new Data.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.data != null) {
      data['data'] = this.data.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Data {
  int id;
  String value;

  Data({this.id, this.value});

  Data.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    value = json['value'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['value'] = this.value;
    return data;
  }
}

class Data {
  String id;
  String value;

  Data({this.id, this.value});

  Data.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    value = json['value'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['value'] = this.value;
    return data;
  }
}

CodePudding user response:

There was a issue you found because loads and date_ranges contain same child name data but there properties are different that's why your model class return error

Here I write a code for loads, please have a look



void main() {

  var data = {
    "loads": {
      "data": [
        {
          "id": 38,
          "value": "#1038 Fort Dodge, IA - Algona, IA"
        },
        {
          "id": 39,
          "value": "#1039 Louisville, KY - Louisville, KY"
        }
      ]
    },
    "date_ranges": {
      "data": [
        {
          "id": "all_time",
          "value": "All"
        },
        {
          "id": "this_year",
          "value": "This year"
        }
      ]
    }
  };
  var loads = Loads.fromJson(data["loads"]);
  
   print(parseData.loads.data[0].id); // return value 38
}

class TestModel {
  Loads loads;
  Loads dateRanges;

  TestModel({this.loads, this.dateRanges});

  TestModel.fromJson(Map<String, dynamic> json) {
    loads = json['loads'] != null ? new Loads.fromJson(json['loads']) : null;
    dateRanges = json['date_ranges'] != null
        ? new Loads.fromJson(json['date_ranges'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.loads != null) {
      data['loads'] = this.loads.toJson();
    }
    if (this.dateRanges != null) {
      data['date_ranges'] = this.dateRanges.toJson();
    }
    return data;
  }
}

class Loads {
  List<Data> data;

  Loads({this.data});

  Loads.fromJson(Map<String, dynamic> json) {
    if (json['data'] != null) {
      data = new List<Data>();
      json['data'].forEach((v) {
        data.add(new Data.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.data != null) {
      data['data'] = this.data.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Data {
  var id; // here changes the type
  String value;

  Data({this.id, this.value});

  Data.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    value = json['value'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['value'] = this.value;
    return data;
  }
}


N.B: Make your id dynamic with var type

  • Related