Usama Hafeez Official
Usama Hafeez Official

Reputation: 321

How to get particular value from JSON object in flutter?

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

Answers (4)

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

Ravindra S. Patil
Ravindra S. Patil

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

quoci
quoci

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

Mozes Ong
Mozes Ong

Reputation: 1294

String jobTitle = json['jobs'][0]['job']['title'];

Upvotes: 4

Related Questions