0
votes

I have to parse the following json to a data model in flutter

[
{
    "cmb_marital_status": {
        "1": "Unmarried",
        "2": " Married",
        "3": " Widow",
        "4": " Divorced",
        "5": " Separated",
        "6": " Living together"
    },
    "cmb_gender": {
        "M": "Male",
        "F": "Female"
    },
    "cmb_yes/no": {
        "1": "No",
        "2": " Yes"
    }
}]

And I have created this data model to parse from the API request.

class AllData {
  CmbMaritalStatus cmbMaritalStatus;
  CmbGender cmbGender;
  CmbYesNo cmbYesNo;


  AllData(
      {this.cmbMaritalStatus,
        this.cmbGender,
        this.cmbYesNo});

  AllData.fromJson(Map<String, dynamic> json) {
    cmbMaritalStatus = json['cmb_marital_status'] != null
        ? new CmbMaritalStatus.fromJson(json['cmb_marital_status'])
        : null;
    cmbGender = json['cmb_gender'] != null
        ? new CmbGender.fromJson(json['cmb_gender'])
        : null;
    cmbYesNo = json['cmb_yes/no'] != null
        ? new CmbYesNo.fromJson(json['cmb_yes/no'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.cmbMaritalStatus != null) {
      data['cmb_marital_status'] = this.cmbMaritalStatus.toJson();
    }
    if (this.cmbGender != null) {
      data['cmb_gender'] = this.cmbGender.toJson();
    }
    if (this.cmbYesNo != null) {
      data['cmb_yes/no'] = this.cmbYesNo.toJson();
    }
    return data;
  }
}

class CmbMaritalStatus {
  String s1;
  String s2;
  String s3;
  String s4;
  String s5;
  String s6;

  CmbMaritalStatus({this.s1, this.s2, this.s3, this.s4, this.s5, this.s6});

  CmbMaritalStatus.fromJson(Map<String, dynamic> json) {
    s1 = json['1'];
    s2 = json['2'];
    s3 = json['3'];
    s4 = json['4'];
    s5 = json['5'];
    s6 = json['6'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['1'] = this.s1;
    data['2'] = this.s2;
    data['3'] = this.s3;
    data['4'] = this.s4;
    data['5'] = this.s5;
    data['6'] = this.s6;
    return data;
  }
}

class CmbGender {
  String m;
  String f;

  CmbGender({this.m, this.f});

  CmbGender.fromJson(Map<String, dynamic> json) {
    m = json['M'];
    f = json['F'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['M'] = this.m;
    data['F'] = this.f;
    return data;
  }
}

class CmbYesNo {
  String s1;
  String s2;

  CmbYesNo({this.s1, this.s2});

  CmbYesNo.fromJson(Map<String, dynamic> json) {
    s1 = json['1'];
    s2 = json['2'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['1'] = this.s1;
    data['2'] = this.s2;
    return data;
  }
}
}

And I am using this API request code to parse data to the data model.

Future <List<AllData>> getAllData(context) async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  final authToken = prefs.getString('accessToken');
  List<AllData> data;
  try{
    final response = await http.get(GET_ALL_DATA,
        headers: {
          HttpHeaders.contentTypeHeader:"application/json",
          HttpHeaders.authorizationHeader: authToken
        },
    );
    var responseBody = json.decode(response.body);
    print("all data response: $responseBody");

    if(response.statusCode == 200){
      var list = json.decode(responseBody[0]) as List;
      data = list.map((i)=> AllData.fromJson(i)).toList();
      return data;
    }else if(response.statusCode == 400 || response.statusCode == 404 || response.statusCode == 401){
      Toast.show("Server error",context,backgroundColor: Colors.red,textColor: Colors.white);
    }else if (response.statusCode == 500){
      Toast.show("Server error",context,backgroundColor: Colors.red,textColor: Colors.white);
    }else{
      Toast.show("Server error",context,backgroundColor: Colors.red,textColor: Colors.white);
    }
  }catch (e){
    Toast.show("Server error",context,backgroundColor: Colors.red,textColor: Colors.white);
  }
  return data;
}

But it is returning this error "type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'"

How can I parse this json successfully?

2

2 Answers

0
votes

Try to get list from responseBody directly:

final responseBody = json.decode(response.body);
print("all data response: $responseBody");

if(response.statusCode == 200) {
  data = responseBody.map((i)=> AllData.fromJson(i)).toList();
  return data;
}
0
votes

First, you already decoded response.body once and you do not need to do it again. Second, responseBody is already List so your code should look like

data = responseBody.map((i)=> AllData.fromJson(i)).toList();
return data;