[ { "Id": 1, "Value": "Planned Annual" }, { "Id": 2, "Value": "Unplanned annual" }, { "Id": 3, "Value": "Sick Leave" }, { "Id": 4, "Value": "Working From Home" }, { "Id": 5, "Value": "Excuse" }, { "Id": 6, "Value": "Maternity leave" }, { "Id": 7, "Value": "Compassionate Leave" }, { "Id": 1002, "Value": "Additional Leave" } ]
CodePudding user response:
create a model as below :
class APIResponse {
int id;
String value;
APIResponse({this.id, this.value});
APIResponse.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;
}
}
Then at API response you can get response like below:
List<APIResposne> list =[];
var result = jsonDecode(response.body);
for(int i=0; i < result.length; i ){
list.add(APIResponse.fromjson(result[i]));
}
CodePudding user response:
for converting JSON into model, use dart::convert library and function JsonDecode.
Lets say, your JSON is in variable "json", then the code would look like this:
Map<String, dynamic> your_model_map = jsonDecode(json);
Hope this answered your question, if not, provide us with more description of your issue.
CodePudding user response:
Make Model from:
Following links:
Generate model class
class APIResponse {
int id;
String value;
APIResponse({this.id, this.value});
APIResponse.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;
}
}
Then map it model class if you are using Dio. no need to decode.
final responseData = response.data;
final List<APIResponse> APIResponseList = responseData.map((item) => APIResponse.fromJson(item)).toList()