-1

Json

{
  "user_id": 7,
  "status": "read",
  "enclosures": [
    {
      "id": 111
    }
  ]
}

I want to get value of 'id' key (111) of "enclosures".

This is how I would get user_id using http package

Map<String, dynamic> decodedData = jsonDecode(res.body);

for (int i = 0; i < decodedData.length; i++) {
  var info = decodedData[i];
  print('${info['user_id']}');
}

But for enclosures, I get null.

Anmol Singh
  • 393
  • 3
  • 16
  • [How can I access and process nested objects, arrays, or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) <-- Javascript, but the syntax/principle is the same – Guy Incognito Mar 28 '23 at 07:40

1 Answers1

0

This works

String json = '''
{
  "user_id": 7,
  "status": "read",
  "enclosures": [
    {
      "id": 111
    },
    {
      "id": 222
    }
  ]
}''';

Map<String, dynamic> decodedData = jsonDecode(json);

for (int i = 0; i < decodedData['enclosures'].length; i++) {
  var info = decodedData['enclosures'][i];
  print('${info['id']}');
}

Note I added a second enclosure to demonstrate that this prints all of them.

Ivo
  • 18,659
  • 2
  • 23
  • 35