kapil singh
kapil singh

Reputation: 136

Convert String To Json with nested array list

I want to convert a string into class object. The example string is Like this :

[{
    "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"
    }]
}]

And I Want to convert the string to the class object. the Class Is

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);
  }
}

I am using a service, but when ever i am trying to use this class to get data it gives me error. My Service Call Is Like This.

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);
    }
  }

I am getting error.

NoSuchMethodError: Class 'MappedListIterable, TrainClassData>' has no instance getter 'ERROR_STATUS'.

Please Help Me. Thanks in advance.

Upvotes: 0

Views: 416

Answers (1)

Yamin
Yamin

Reputation: 3018

There were some personal mistakes that cause issues.

1) Dart convert package takes care of casting the data. So You should use the proper type for the properties. (e.x. ERROR_STATUS should be bool, not string).

2) The response is an array. you should check types and classes you use. the corrected example is here:

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);
  }
}

And for the service:

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 = [];
    }

Upvotes: 1

Related Questions