0

This is my JSON data:

{ 
   "result":"0",
   "school_code":"School Code",
   "dashboard":{ 
      "resultPrivilege":"0",
      "privilege":[ 
         { 
            "activity_id":"activity_id",
            "privilege_id":"privilege_id"
         }
      ]
   }
}

I want to retrieve privilege array from this Json data.
Print privilege array on terminal.
How to retrieve JSON object of privilege array

This is my code to retrieve JSON data:

var data = json.decode(response.body);
    var resultCheck = data['result'];
    if (resultCheck == '0') {
      var rest = data["dashboard"];
      debugPrint(rest);
}

output of this code:

result check 0
I/flutter (18905): {"privilege":[
{"activity_id":165,"privilege_id":1568}

I want to output this format when i print privilege:

I/flutter (18905):[{"activity_id":165,"privilege_id":1568}]

How to get only privilege array on terminal also how to print object of privilege?.

0

3 Answers 3

1
Map previlege = {
    'result': '0',
    'school_code': 'School Code',
    'dashboard': {
      'resultPrivilege': '0',
      'privilege': [
        {
          'activity_id': 'activity_id',
          'privilege_id': 'privilege_id',
        }
      ],
    }
  };
  print('Reuslt:  ${previlege['result']}' +
      '\nSchool Code:  ${previlege['school_code']}' +
      '\nActivity ID:  ${previlege['dashboard']['privilege'][0]['activity_id']}');

Screenshot

Sign up to request clarification or add additional context in comments.

4 Comments

I attached screenshot too ! How you are trying ?
you declare json data in map privilege but i want to fetch json data from api
http.Response response = await http.post(apiUrl, body: body, headers: headers); // print(response.body); Map data = json.decode(response.body); print(data); print('Reuslt: ${data['result']}' + '\nSchool Code: ${data['school_code']}' + '\nActivity ID: ${data['dashboard']['privilege'][0]['activity_id']}');
change your code here--> var rest = data["dashboard"]; to var rest = data["dashboard"]['privilege'];
1
import 'dart:convert';

void main() {
  var jsonData='{"result":"0","school_code":"School Code","dashboard":{"resultPrivilege":"0", "privilege": [{"activity_id":"activity_id","privilege_id":"privilege_id"}]}}';
  var json=jsonDecode(jsonData);
  json['dashboard']['privilege'].forEach((obj){
    //
    print(obj.toString());
  });
}

Comments

0

Try this,

Map<String, dynamic> jsonBody = json.decode(response.body);

String result = jsonBody["result"];
if (result == '0') {
  Map<String, dynamic> dashboard = jsonBody["dashboard"];
  List privilege = dashboard["privilege"];
  debugPrint(privilege.toString());
}

or you can easily create PODO(Plain Old Dart Objects) classes using quicktype or JsonToDart online tools. Then you can convert JSON to dart class objects.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.