0

How do I parse a complex JSON into a list in Darts/flutter

I have the message in a map but I need some help parsing and getting the value

This is the JSON:..

 {
        "jobs": [
            {
                "id": "S_1244",
                "title": "Title1",
                "location": {
                    "city": "Miami",
                    "stateCode": "FL"

                },
                "salary": {
                    "symbol": "US$",
                    "min": "26.15",
                    "max": "27.15"
                },
                "type": "Temporary",
                "posted": 1530027914570

            },

             {
                "id": "S_1234",
                "title": "Title1",
                "location": {
                    "city": "Miami",
                    "stateCode": "FL"

                },
                "salary": {
                    "symbol": "US$",
                    "min": "26.15",
                    "max": "27.15"
                },
                "type": "Temporary",
                "posted": 1530027914570

            }
       ]
 }

I have the body in a map

Map map = jsonDecode(data.body);

Thanks for your help

2 Answers 2

1

You can choose to model everything in your json. Here's the code for start.

  Map myMap = json.decode(response.body);
  Iterable i = myMap['jobs'];
  List<Jobs> jobs = i.map((model) => Jobs.fromJson(model)).toList();
}

class Jobs {
  Jobs({this.id, this.title, this.type});
  String id;
  String title;
  String type;

  Jobs.fromJson(Map<String, dynamic> json)
      : id = json['id'],
        title = json['title'],
        type = json['type'];
}

You will end up List of Jobs, which is plain old java class representation of your jobs in your json. You can model location and salary too.

Sign up to request clarification or add additional context in comments.

Comments

0

There may be a cleaner way to do this with mappings, but seems to work.

  Map map = jsonDecode(data.body);
  List jobList = map["jobs"];
  for ( var job in jobList ) {
    print("id= ${job['id']} title=${job['title']}   location=${job['location']['city']} ");
  }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.