Is it possible to have one function to read any json file with any structure? The application will read different json data that are not known in advance, I need to have a function that can process any json file.
Is it possible to have one function to read any json file with any structure? The application will read different json data that are not known in advance, I need to have a function that can process any json file.
Using the dart_convert library you can parse a json string to a map using Map<String, dynamic> yourMap = jsonDecode(jsonString);
First you should build a Json Model and then convert it to object
Json :
[{"code": 401,"message": "No data found"}]
json to model :
class ResponseModel {
int code;
String message;
ResponseModel({this.code, this.message});
ResponseModel.fromJson(Map<String, dynamic> json) {
code = json['code'];
message = json['message'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['code'] = this.code;
data['message'] = this.message;
return data;
}
}
convert json to Object LIst
List<ResponseModel> lists = List<ResponseModel>.from(json.decode(response.body).map((x) => ResponseModel.fromJson(x)));