使用嵌套数组列表将字符串转换为Json

时间:2019-04-15 07:15:07

标签: dart flutter

我想将字符串转换为类对象。示例字符串如下所示:

[{
    "MESSAGE": "Successfully!",
    "ORIGINAL_ERROR": "",
    "ERROR_STATUS": false,
    "RECORDS": true,
    "Data": [{
        "Id": "1",
        "Name": "Third AC"
    }, {
        "Id": "2",
        "Name": "Second AC"
    }, {
        "Id": "3",
        "Name": "First AC"
    }, {
        "Id": "4",
        "Name": "AC Chair Car"
    }, {
        "Id": "5",
        "Name": "First Class"
    }, {
        "Id": "6",
        "Name": "Sleeper Class"
    }, {
        "Id": "7",
        "Name": "Second Class Unreserved"
    }, {
        "Id": "8",
        "Name": "3AC Economy "
    }, {
        "Id": "9",
        "Name": "Second Seating"
    }, {
        "Id": "10",
        "Name": "Executive Class"
    }]
}]

我想将字符串转换为类对象。类是

class TrainClassData {
  String MESSAGE;
  String ORIGINAL_ERROR;
  String ERROR_STATUS;
  String RECORDS;
  List<TrainClass> Data;

  TrainClassData({
    this.MESSAGE,
    this.ORIGINAL_ERROR,
    this.ERROR_STATUS,
    this.RECORDS,
    this.Data,
  });

  factory TrainClassData.fromJson(Map<String, dynamic> json) {
    return TrainClassData(
      MESSAGE: json['Id'] as String,
      ORIGINAL_ERROR: json['Name'] as String,
      ERROR_STATUS: json['Name'] as String,
      RECORDS: json['Name'] as String,
      Data: json['Data'].map<TrainClass>((json) => TrainClass.fromJson(json)).toList()
    );
  }
}

class TrainClass {
  String Id;
  String Name;

  TrainClass({
    this.Id,
    this.Name,
  });

  factory TrainClass.fromJson(Map<String, dynamic> json) {
    return TrainClass(
        Id: json['Id'] as String, Name: json['Name'] as String);
  }
}

我正在使用服务,但是每当我尝试使用此类获取数据时,都会给我错误。我的服务电话就是这样。

static Future<List<TrainClassData>> GetTrainClass() async {
    String url = 'http://ttreturn.itfuturz.com/AppService.asmx/TrainClassMaster?type=trainclass';
    print("train class URL: " + url);
    final response = await http.get(url);
    try {
      if (response.statusCode == 200) {
        List<TrainClassData> list = [];
        if (response.body != "" && response.body.toString() != "[]") {
          final parsed = json.decode(response.body).cast<Map<String, dynamic>>();
          var trainClassData = parsed.map<TrainClassData>((json) => TrainClassData.fromJson(json));
          if(trainClassData.ERROR_STATUS == false) {
            list = trainClassData.Data;
          }
          else {
            list = [];
          }
        }
        return list;
      } else {
        throw Exception(MESSAGES.INTERNET_ERROR);
      }
    } catch (e) {
      print("Kapil Erorr : " + e.toString());
      throw Exception(MESSAGES.INTERNET_ERROR);
    }
  }

我遇到错误。

  

NoSuchMethodError:类'MappedListIterable,   TrainClassData>'没有实例获取器'ERROR_STATUS'。

请帮助我。预先感谢。

1 个答案:

答案 0 :(得分:1)

有些人为错误会引起问题。

1)Dart convert包负责转换数据。因此,您应该为属性使用正确的类型。 (例如ERROR_STATUS应该是bool,而不是字符串)。

2)响应是一个数组。您应该检查所使用的类型和类。更正的示例在这里:

class TrainClassData {
  String MESSAGE;
  String ORIGINAL_ERROR;
  bool ERROR_STATUS;

  bool RECORDS;
  List<TrainClass> Data;

  TrainClassData({
    this.MESSAGE,
    this.ORIGINAL_ERROR,
    this.ERROR_STATUS,
    this.RECORDS,
    this.Data,
  });

  factory TrainClassData.fromJson(Map<String, dynamic> json) {
    return TrainClassData(
        MESSAGE: json['MESSAGE'] as String,
        ORIGINAL_ERROR: json['ORIGINAL_ERROR'] as String,
        ERROR_STATUS: json['ERROR_STATUS'] as bool,
        RECORDS: json['RECORDS'] as bool,
        Data: json['Data']
            .map<TrainClass>((json) => TrainClass.fromJson(json))
            .toList());
  }
}

class TrainClass {
  String Id;
  String Name;

  TrainClass({
    this.Id,
    this.Name,
  });

  factory TrainClass.fromJson(Map<String, dynamic> json) {
    return TrainClass(Id: json['Id'] as String, Name: json['Name'] as String);
  }
}

对于服务:

List<TrainClass> list = [];
    final parsed = json.decode(response.body).cast<Map<String, dynamic>>();
    var trainClassData = parsed
        .map<TrainClassData>((json) => TrainClassData.fromJson(json))
        .toList();
    if (trainClassData[0].ERROR_STATUS == false) {
      list = trainClassData[0].Data;
    } else {
      list = [];
    }