Reputation: 321
I wanted to get value from the API JSON object. My API JSON response is like this.
{
"jobs": [
{
"id": 1,
"user_id": "10",
"job_id": "1",
"experience": "12",
"salary": "1122378"
"job": {
"id": 1,
"title": "PHP SENIOR DEVELOPER",
"company_name": "Envato",
}
}
]
}
I wanted to get the title from the job JSON object. How can we get this value in flutter?
Upvotes: 3
Views: 6221
Reputation: 510
You can use https://app.quicktype.io/ to generate the deserialization, so you can handle it by models to prevent errors. Check: https://flutter.dev/docs/development/data-and-backend/json
Upvotes: 0
Reputation: 14775
if you get title of the job inside jobs section try below code :
var response = jsonDecode(response.body);
var title = response['jobs'][0]['job']['title'];
Upvotes: 1
Reputation: 3557
You can achieve this by doing the followng
final Map<String, dynamic> response = {
"jobs": [
{
"id": 1,
"user_id": "10",
"job_id": "1",
"experience": "12",
"salary": "1122378",
"job": {
"id": 1,
"title": "PHP SENIOR DEVELOPER",
"company_name": "Envato",
}
}
],
};
final List<Map<String, dynamic>> jobs = response['jobs'];
final Map<String, dynamic> job = jobs.first['job'];
final String jobTitle = job['title'];
print(jobTitle);
Upvotes: 0