Home > Mobile >  GET API using Flutter From Nested Structure of JSON
GET API using Flutter From Nested Structure of JSON

Time:01-28

so here I've just learned about parsing API using Flutter. So the problem is, how can i use this code below but use the API/JSON structure which I have included too?

Here's the code :

class TriedGetData extends StatelessWidget {
  final String apiUrl = "https://reqres.in/api/users?per_page=15";
  Future<List<dynamic>> _fecthDataUsers() async {
    var result = await http.get(apiUrl);
    return json.decode(result.body)['data'];
  }

The json that i want to use :

{
  "x": {
    "y": {
      "a": "27 Jan 2022",
      "b": "20:20:02 WIB",
   }
}

and, I've already done like this. But still, it doesn't work,

Future<List<dynamic>> _fecthDataUsers() async {
    var result = await http.get(apiUrl);
    var data = json.decode(result.body)['x'];
    return data['y'];
  }
     

CodePudding user response:

Instead of

return data['y'];

try

return data[0];

as 'x' or data is a list and y is the 1st element in list , we should specify the index for it.

CodePudding user response:

For starters, that isn't a valid JSON format. If you're ever unsure, you can run it through JSON Lint

For the sake of this example, I turned it into a valid JSON object by simply adding a closing brace at the end:

{
    "x": {
        "y": {
            "a": "27 Jan 2022",
            "b": "20:20:02 WIB"
        }
    }
}

Then it should be a simple key mapping. You just missed the first level is all:

data["x"]["y"]

Put into practice, the end result looks something like this:

import 'dart:io';
import 'dart:convert';

void main() async {

  var request = await HttpClient().getUrl(Uri.parse("https://reqres.in/api/users?per_page=5&page=1"));
  var response = await request.close();

  await for (var contents in response.transform(Utf8Decoder())) {
    var jsondata = jsonDecode(contents);
    var targetdata = jsondata["data"][0]["email"];
  }
}

The full decoded JSON data:

{
    page: 1,
    per_page: 5,
    total: 12,
    total_pages: 3,
    data: [{
                id: 1,
                email: george.bluth @reqres.in,
                first_name: George,
                last_name: Bluth,
                avatar: https: //reqres.in/img/faces/1-image.jpg}, {id: 2, email: [email protected], first_name: Janet, last_name: Weaver, avatar: https://reqres.in/img/faces/2-image.jpg}, {id: 3, email: [email protected], first_name: Emma, last_name: Wong, avatar: https://reqres.in/img/faces/3-image.jpg}, {id: 4, email: [email protected], first_name: Eve, last_name: Holt, avatar: https://reqres.in/img/faces/4-image.jpg}, {id: 5, email: [email protected], first_name: Charles, last_name: Morris, avatar: https://reqres.in/img/faces/5-image.jpg}], support: {url: https://reqres.in/#support-heading, text: To keep ReqRes free, contributions towards server costs are appreciated!}}

The specific value you want to target (targetdata):

[email protected]

  •  Tags:  
  • Related