使用Dart将String转换为List / Map

时间:2019-12-15 09:54:51

标签: flutter dart dart-html

我正在尝试将externalHTML转换为JSON怎样转换此字符串

<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> 

使用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 个答案:

答案 0 :(得分: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);




}