Reputation: 2443
When I print out print(response.body), I am a getting a json data like this:
{
"kind": "youtube#videoListResponse",
"etag": "BaEhOMCS17JNvG29RdTWOMEWXcM",
"items": [
{
"kind": "youtube#video",
"etag": "jUDRGv06hm-CRn0BiOCPfDAB_ww",
"id": "zVGjcT-w2R0",
"snippet": {
"publishedAt": "2021-02-20T16:00:32Z",
"channelId": "UCCkcCJBEKZ0sLKVYxlN1fkw",
"title": "SHES GONE | Little Nightmares 2 Gameplay Walkthrough Part 5 THE END",
"description": "The Scariest of The Littlest of Nightmares\nor just a scary little game \nThis Is The Ending Of Little Nightmares 2",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/zVGjcT-w2R0/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/zVGjcT-w2R0/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/zVGjcT-w2R0/hqdefault.jpg",
"width": 4<…>
From this json object, how can I get the title from this json data.
Upvotes: 0
Views: 81
Reputation: 6134
What you're trying to do is called json deserialization / json decoding
. You need to decode the json string into a Map<String,dynamic>
:
final parsed = jsonDecode(response.body);
Now that we have the values in a Map<String,dynamic>
. You could loop through your item
array and get the title's
value:
for (int i = 0; i < parsed["items"].length; i++) {
print(parsed["items"][i]["snippet"]["title"]);
}
Here is a the flutter documentation about how to deserialize json: https://flutter.dev/docs/cookbook/networking/background-parsing
Upvotes: 1
Reputation: 6239
Your data is not complete, even so I'm supposing the starting data type is Map<String, dynamic>
, then this snippet should work:
var data = {"kind": "...", "etag": "...", "items": [...]};
var title = (((data['items'] as List).first as Map)['snippet'] as Map)['title'];
Upvotes: 1