title: Flutter - Json array of objects
body: <p>How would I parse a nested array of objects such as the one below.</p>
  
  <pre><code>{
      "status": "success",
      "code": 200,
      "message": "The request was successful",
      "data": [
          {
              "name": "Abu Dhabi",
              "id": 4139,
              "parent": 5153,
              "type": "city",
              "imageURL": ""
  
          },
          {
              "name": "Croatia",
              "id": 5037,
              "parent": 6886,
              "type": "country",
              "imageURL": ""
  
          },
       ]
  }
  </code></pre>
  
  <p>I am currently making an API call which returns data in the format as shown above. </p>
  
  <p>My api call is as follows: </p>
  
  <pre><code>  Future&lt;Location&gt; getLocations() async {
      final response =
          await http.get('$endpoint/locations', headers: authenticatedHeader);
  
      if (response.statusCode == 200) {
        final responseJson = json.decode(response.body);
  
        // If server returns an OK response, parse the JSON.
  
        return Location.fromJson(responseJson);
      } else {
        // If that response was not OK, throw an error.
        throw Exception('Failed to load post');
      }
    }
  </code></pre>
  
  <p>I then have a location class as below:</p>
  
  <pre><code>class Location {
    String name;
    int id;
    int parent;
    String type;
    String imageURL;
  
    Location(
        {this.name, this.id, this.parent, this.type, this.imageURL});
  
    factory Location.fromJson(Map&lt;String, dynamic&gt; json) =&gt;
        _locationFromJson(json);
  }
  
  Location _locationFromJson(Map&lt;String, dynamic&gt; json) {
    return Location(
      name: json['name'] as String,
      id: json['id'] as int,
      parent: json['parent'] as int,
      type: json['type'] as String,
      imageURL: json['imageURL'] as String
    );
  }
  </code></pre>
  
  <p>I am hoping to be able to retrieve all the locations above using a listview builder and create a listtile for each location.</p>
  
  <p>How can I parse the JSON correctly?</p>