1

This json response work fine with below List.

[{"id":"1","ename":"KING","sal":"5000"............

List<Emp> emp= (json.decode(response.body) as List)
        .map((data) => Emp.fromJson(data))
        .toList();

But how in list load data when on start json stay items

{"items":[{"id":1,"ename":"KING","sal":5000............

2 Answers 2

1

Use json.decode(response.body)['items'] instead of json.decode(response.body)

An example:

import 'dart:convert';

void main() {
  
  // sample body.response

  final j = '''{
  "items": [
  {"id":"1","ename":"KING","sal":"5000"},
  {"id":"2","ename":"KING","sal":"5000"}
  ]
  }''';

  List<Emp> emp = (json.decode(j)['items'] as List)
      .map((data) => Emp.fromJson(data))
      .toList();

  print(emp);
}

class Emp {
  String id;
  String ename;
  String sal;

  Emp({this.id, this.ename, this.sal});

  Emp.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    ename = json['ename'];
    sal = json['sal'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['ename'] = this.ename;
    data['sal'] = this.sal;
    return data;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1
List<Emp> emp= (json.decode(response.body)["items"] as List)
        .map((data) => Emp.fromJson(data))
        .toList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.