-1

I have a json response like this,

[{"name":"Bangladesh","info":"{\"summary\": [[\"DataSource\", \"Worldometers\"], [\"Country\", \"Bangladesh\"], [\"Total Cases\", 112306], [\"New Cases(24hrs)\", \"0\"], [\"Deaths\", 1464], [\"New Deaths(24hrs)\", \"0\"], [\"Recovered\", 45077], [\"Active\", 65765], [\"Total Tests\", 615164]], \"test\": [[\"Updated on 21-05-2020\", \"Total\"], [\"Test conducted (24hrs)\", \"10262\"], [\"Total test conducted\", \"214114\"], [\"Positive cases (24hrs)\", \"1773\"], [\"Confirmed\", \"28511\"]]}"}]

How can I parse this and save the data fields in such a manner so that I can use those as a variables like deaths, newCases etc in other widgets?

2 Answers2

0
    import 'dart:convert';

class Data {
  Data({
    this.name,
    this.info,
  });

  String name;
  Info info;

  factory Data.fromJson(Map<String, dynamic> json) {
    final _data = jsonDecode(json['info']);
    return Data(
      name: json['name'],
      info: Info.fromJson(_data),
    );
  }
}

class Info {
  Info({
    this.summary,
    this.test,
  });

  List<List<dynamic>> summary;
  List<List<String>> test;
  factory Info.fromJson(Map<String, dynamic> json) {
    return Info(
      summary: List<List<dynamic>>.from(json['summary'].map(
          (element) => List<dynamic>.from(element.map((element) => element)))),
      test: List<List<String>>.from(json['test'].map(
          (element) => List<String>.from(element.map((element) => element)))),
    );
  }
}



 const data = {
  "name": "Bangladesh",
  "info":
      "{\"summary\": [[\"DataSource\", \"Worldometers\"], [\"Country\", \"Bangladesh\"], [\"Total Cases\", 112306], [\"New Cases(24hrs)\", \"0\"], [\"Deaths\", 1464], [\"New Deaths(24hrs)\", \"0\"], [\"Recovered\", 45077], [\"Active\", 65765], [\"Total Tests\", 615164]], \"test\": [[\"Updated on 21-05-2020\", \"Total\"], [\"Test conducted (24hrs)\", \"10262\"], [\"Total test conducted\", \"214114\"], [\"Positive cases (24hrs)\", \"1773\"], [\"Confirmed\", \"28511\"]]}"
};
darkness
  • 183
  • 1
  • 10
  • How do I get the value of each elements like Deaths, or Recovered and save them into variables? Besides, how do I connect this two model class with my code? – Moshiur Rahman Jun 22 '20 at 08:39
  • You can get the field value through index. final data = Data.fromJson(data); var deaths = data.info.summary[4][1]. – darkness Jun 22 '20 at 08:45
  • For example, I have a api class for calling the response. Then after getting the response, how do I send it to your described class? And, then how do I get the value of each different keys so that I can use those values in some other classes? – Moshiur Rahman Jun 22 '20 at 08:46
  • You should be create a new class to contains all field. Example class Detail{ String country; Detail({this.country}); factory Detail.fromList(List> list){ return Detail( country: list[0][1], ); } } – darkness Jun 22 '20 at 09:02
-1

You can use the 'convert' library of dart. Its pretty simple, you just need to encode your data in map format. for more details visit dart:convert documentation

ARAB KUMAR
  • 86
  • 1
  • 8