Zaza.codes
Zaza.codes

Reputation: 327

How to skip over specific keys in a Json map in flutter

Data got from my api looks like this and I need to get the elements all inside "5f1916a05bc6cb3f055c20bc" without doing jsonResponse["5f1916a05bc6cb3f055c20bc"]["video"] because the value of "5f1916a05bc6cb3f055c20bc" changes per item. Are there any ways that I can create a model for this?

{
    "5f1916a05bc6cb3f055c20bc": "{
        "video": "",
        "image": "",
        "likes": 0,
        "dislikes": 0,
        "trash": 0,
        "createdAt": "2020-07-23T04: 48: 00.000Z",
        "id": "5f1916a05bc6cb3f055c20bc",
        "author": "5eeb7edbac4dba7b6d3e68c1",
        "userTag": "@doeee",
        "text": "Checking again",
        "campus": "University Of Technology",
        "__v": 0
    }
}

Upvotes: 2

Views: 1943

Answers (3)

Sagar Acharya
Sagar Acharya

Reputation: 3777

Just check out this code and let me know if it works: This will dynamically get the key value pairs:

import 'dart:convert';

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: HomePage());
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    super.initState();
    getData();
  }

  getData() async {
    String data =
        await DefaultAssetBundle.of(context).loadString("json/parse.json");

    var jsonData = json.decode(data);

    jsonData.forEach((key, value) {
      if (key != "socketID") {
        print('This is key : $key');
        print('This is the value : $value');
        // Following are the specific object value:

        value.forEach((key, value) {
          print('$key');
          print('$value');
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: Text(''));
  }
}

Let me know if it works.

Upvotes: 1

Vasanth Vadivel
Vasanth Vadivel

Reputation: 134

var Response =await http.get("Your API URL",headers: {"Accept": "application/json"},);
if (Response.statusCode == 200) 
{
var data = json.decode(Response.body);
var result=Object.keys(data);
console.log(Object.keys(result));// Array ["socketID", "5f1916a05bc6cb3f055c20bc"]
console.log(result[1]); //"5f1916a05bc6cb3f055c20bc"
}

Now your required key is in result[1],just use this and parse data

Upvotes: 0

Vasanth Vadivel
Vasanth Vadivel

Reputation: 134

const object1={  
 "socketID": "H7Cddg9o6rbyvB_TAAAC",
    "5f1916a05bc6cb3f055c20bc":{
        "video": "",
        "image": "",
        "likes": 0,
        "dislikes": 0,
        "trash": 0,
        "createdAt": "2020-07-23T04: 48: 00.000Z",
        "id": "5f1916a05bc6cb3f055c20bc",
        "author": "5eeb7edbac4dba7b6d3e68c1",
        "userTag": "@doeee",
        "text": "Checking again",
        "campus": "University Of Technology",
        "__v": 0.
    }
}

Var Z=Object.keys(object1); You can print them in console to check the result

Console.log(Z);

Now this Z will hold your array of Keys in your response,then you can use For loop to get specific key value based on their Index

Upvotes: 0

Related Questions