1

I'm trying to convert a outerHTML into a JSON How can I convert this String

<tr>
                     <td>00001</td>
                     <td>ADVANCE DATABASE MANAGEMENT SYSTEM [A]</td>
                     <td>Open</td>
                     <td>40</td>
                     <td>37</td>

                     <td><table class="table table-condensed table-bordered"><tbody><tr><td>Theory </td> <td>Sunday </td> <td>8:00 AM </td> <td>10:00 AM </td> <td>1113 </td></tr><tr><td>Lab </td> <td>Tuesday </td> <td>8:00 AM </td> <td>11:00 AM </td> <td>D0202 </td></tr></tbody></table></td>
                 </tr> 

into List or Map like this using Dart/Flutter

{
    "class id": "00001",
    "title": "ADVANCE DATABASE MANAGEMENT SYSTEM [A]",
    "status": "Open",
    "capcity": "40",
    "count": "37",
    "time": {
        "Theory": {
            "day": "Sunday",
            "start": "8:00 AM",
            "end": "10:00 AM",
            "location": "1113"
        },
        "Lab": {
            "day": "Tuesday",
            "start": "8:00 AM",
            "end": "11:00 AM",
            "location": "D0202"
        }
    }
},

1 Answer 1

1
import 'package:html/parser.dart' show parse;


main() {
  var convertedList={};
  var document = parse(
      """<table><tr>
                     <td>00001</td>
                     <td>ADVANCE DATABASE MANAGEMENT SYSTEM [A]</td>
                     <td>Open</td>
                     <td>40</td>
                     <td>37</td>

                     <td><table class="table table-condensed table-bordered"><tbody><tr><td>Theory </td> <td>Sunday </td> <td>8:00 AM </td> <td>10:00 AM </td> <td>1113 </td></tr><tr><td>Lab </td> <td>Tuesday </td> <td>8:00 AM </td> <td>11:00 AM </td> <td>D0202 </td></tr></tbody></table></td>
                 </tr> </table>""");
 var cells=document.getElementsByTagName('table')[0].getElementsByTagName('td');                
 var theoryCells=document.getElementsByTagName('table')[1].getElementsByTagName('tr')[0].getElementsByTagName('td');
 var labCells=document.getElementsByTagName('table')[1].getElementsByTagName('tr')[1].getElementsByTagName('td');

  convertedList['classId']=cells[0].innerHtml;
  convertedList['title']=cells[1].innerHtml;
   convertedList['status']=cells[2].innerHtml;
  convertedList['capcity']=cells[3].innerHtml;
  convertedList['count']=cells[4].innerHtml;

  convertedList['time']={
    'theory':{
       "day":  theoryCells[1].innerHtml,
        "start":  theoryCells[2].innerHtml,
        "end":  theoryCells[3].innerHtml,
        "location": theoryCells[4].innerHtml
    },
    'lab':{
       "day":  labCells[1].innerHtml,
        "start":  labCells[2].innerHtml,
        "end":  labCells[3].innerHtml,
        "location": labCells[4].innerHtml
    }

  };


  print(convertedList);




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

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.