processJsonData function

void processJsonData(
  1. Map<String, dynamic> jsonData
)

Implementation

void processJsonData(Map<String, dynamic> jsonData) {
  // Extract the data from the message object
  Map<String, dynamic> dataObject = jsonData['message']['data'];

  // Initialize result array
  List<Map<String, dynamic>> resultArray = [];

  // Extract options and result from dataObject
  Map<String, dynamic> options = dataObject['options'];
  Map<String, dynamic> result = dataObject['result'];

  // Extract total result
  double totalResult = (dataObject['total_result'] as int).toDouble();

  // Iterate over options to calculate percentage
  options.forEach((key, value) {
    int optNo = int.parse(key.replaceAll('opt', '')); // Convert 'opt1' to 1, 'opt2' to 2, etc.
    double vote = (result['opt$optNo'] as int).toDouble();

  });

  // Final result object with data and question
  Map<String, dynamic> resultObject = {
    'data': resultArray,
    'ques': dataObject['question'],
  };

  // Output the resultObject (you can handle it as needed)
  print(jsonEncode(resultObject));
}