Flutter Json解析工具

import 'dart:convert';
import 'dart:io';

Future main() async {
  String filePath = "D:\\FlutterProject\\suzhou_bridge_2023\\lib\\services"
      "\\frequent_inspection\\frequent_inspection_disease_data_test.dart";
  var file = File(filePath);

  List classNames = [];

  Stream> inputStream = file.openRead();
  List trueLines = [];

  var lines = utf8.decoder
      .bind(inputStream)
      .transform(const LineSplitter());
  try {
    await for (final line in lines) {
      trueLines.add(line);
      if(RegExp(r'class [a-zA-Z0-9]* extends Object').hasMatch(line)){
        classNames.add(line.split(" ")[1]);
      }
    }
    // print(classNames);
    // print(trueLines);
    String fileStr = "import 'dart:core';\nimport 'package:json_annotation/json_annotation.dart';\n\n";
    for(var className in classNames){
      fileStr = fileStr+_getClassAndPara(className,trueLines);
    }
    // print(fileStr);
    await file.delete();
    try{
      File file2 = File(filePath);
      await file2.writeAsString(fileStr);
    }catch (e) {
      // print(e);
    }
  } catch (e) {
    // print(e);
  }
}

String _getClassAndPara(String classN,List trueLines){
  int startMark = 0;
  int endMark = 0;
  List paras = [];
  for(int i=0;i').hasMatch(para)){
      fromJson = "$fromJson  ${para.split(" ")[0]} ${para.split(" ")[1]};\n";
      fromJson = "$fromJson  if(json['${para.split(" ")[1]}'] == null){\n    ${para.split(" ")[1]} = [];\n  }\n";
      if(para.contains("String?")){
        fromJson = "$fromJson  else{\n    ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:e as String).toList();\n  }\n\n";
      }
      else if(para.contains("int?")){
        fromJson = "$fromJson  else{\n    ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:e as int).toList();\n  }\n\n";
      }
      else if(para.contains("double?")){
        fromJson = "$fromJson  else{\n    ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:e as double).toList();\n  }\n\n";
      }
      else if(para.contains("bool?")){
        fromJson = "$fromJson  else{\n    ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:e as bool).toList();\n  }\n\n";
      }
      else{
        fromJson = "$fromJson  else{\n    ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:${para.split(" ")[0].split("<")[1].split("?")[0]}.fromJson(e as Map)).toList();\n  }\n\n";
      }
    }
  }

  fromJson = "$fromJson  return $classN(\n";


  for(var para in paras){
    if(RegExp(r'List<[a-zA-Z]*\??>').hasMatch(para)){
      fromJson = "$fromJson    ${para.split(" ")[1]},\n";
    }else if(para.contains("String?")||para.contains("int?")||para.contains("double?")||para.contains("bool?")){
      fromJson = "$fromJson    json['${para.split(" ")[1]}'],\n";
    }else{
      fromJson = "$fromJson    ${para.split("?").first}.fromJson(json['${para.split(" ")[1]}']),\n";
    }
    toJson = "$toJson        '${para.split(" ")[1]}': instance.${para.split(" ")[1]},\n";
  }
  fromJson = "$fromJson  );\n}\n";
  toJson = "$toJson    };\n\n";


  String lastStr = "";
  for(int i=startMark;i 
  
import 'dart:convert';
import 'dart:io';

Future main() async {
  String filePath = "D:\\FlutterProject\\suzhou_bridge_2023\\lib\\services"
      "\\frequent_inspection\\frequent_inspection_disease_data_test.dart";
  var file = File(filePath);

  List classNames = [];

  Stream> inputStream = file.openRead();
  List trueLines = [];

  var lines = utf8.decoder
      .bind(inputStream)
      .transform(const LineSplitter());
  try {
    await for (final line in lines) {
      trueLines.add(line);
      if(RegExp(r'class [a-zA-Z0-9]* extends Object').hasMatch(line)){
        classNames.add(line.split(" ")[1]);
      }
    }
    // print(classNames);
    // print(trueLines);
    String fileStr = "import 'dart:core';\nimport 'package:json_annotation/json_annotation.dart';\n\n";
    for(var className in classNames){
      fileStr = fileStr+_getClassAndPara(className,trueLines);
    }
    // print(fileStr);
    await file.delete();
    try{
      File file2 = File(filePath);
      await file2.writeAsString(fileStr);
    }catch (e) {
      // print(e);
    }
  } catch (e) {
    // print(e);
  }
}

String _getClassAndPara(String classN,List trueLines){
  int startMark = 0;
  int endMark = 0;
  List paras = [];
  for(int i=0;i').hasMatch(para)){
      fromJson = "$fromJson  ${para.split(" ")[0]} ${para.split(" ")[1]};\n";
      fromJson = "$fromJson  if(json['${para.split(" ")[1]}'] == null){\n    ${para.split(" ")[1]} = [];\n  }\n";
      if(para.contains("String?")){
        fromJson = "$fromJson  else{\n    ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:e as String).toList();\n  }\n\n";
      }
      else if(para.contains("int?")){
        fromJson = "$fromJson  else{\n    ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:e as int).toList();\n  }\n\n";
      }
      else if(para.contains("double?")){
        fromJson = "$fromJson  else{\n    ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:e as double).toList();\n  }\n\n";
      }
      else if(para.contains("bool?")){
        fromJson = "$fromJson  else{\n    ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:e as bool).toList();\n  }\n\n";
      }
      else{
        fromJson = "$fromJson  else{\n    ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:${para.split(" ")[0].split("<")[1].split("?")[0]}.fromJson(e as Map)).toList();\n  }\n\n";
      }
    }
  }

  fromJson = "$fromJson  return $classN(\n";


  for(var para in paras){
    if(RegExp(r'List<[a-zA-Z]*\??>').hasMatch(para)){
      fromJson = "$fromJson    ${para.split(" ")[1]},\n";
    }else if(para.contains("String?")||para.contains("int?")||para.contains("double?")||para.contains("bool?")){
      fromJson = "$fromJson    json['${para.split(" ")[1]}'],\n";
    }else{
      fromJson = "$fromJson    ${para.split("?").first}.fromJson(json['${para.split(" ")[1]}']),\n";
    }
    toJson = "$toJson        '${para.split(" ")[1]}': instance.${para.split(" ")[1]},\n";
  }
  fromJson = "$fromJson  );\n}\n";
  toJson = "$toJson    };\n\n";


  String lastStr = "";
  for(int i=startMark;i

你可能感兴趣的:(flutter,json)