skip to Main Content

I am trying to parse JSON in flutter. I tried with below code.

Sample JSON.

{
    "statusCode": 200,
    "message": "",
    "result": [
        {         
            "userName": "RAJ",
            "hierarchyLevel": 3,
            "sequence": 1,        
            "isSuccess": 1
        },
        {
            "userName": "SAM",
            "hierarchyLevel": 3,
            "sequence": 1,        
            "isSuccess": 1 
        },
       
    ]
}

My Code is:

                  int jsonStatus = response.statusCode;
                  if (jsonStatus == 200) {
                    var jsonData = json.decode(response.body);
                    if (jsonData["statusCode"] == 200) {
                      List<dynamic> listValJson = jsonData["result"];
                      List<String> userList = new List<String>.from(listValJson);
                    }
          }

I am getting below error:

Error: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'

I know it is basic question for JSON parsing. I tried with Mapping also. But in this case I need list of "userName"’s presented in array. Kindly check my JSON

3

Answers


  1. This error is simply says that you are trying to convert Map into String.

    List<dynamic> listValJson = jsonData["result"];
    

    now ‘listValJson’ is List<Map<String,dynamic>> not List<String>

    Try to create a data model for all user’s data or use

    if (jsonData["statusCode"] == 200) {
                      List<dynamic> listValJson = jsonData["result"];
                      List<String> userList = new List<String>.from(listValJson['userName']);
                    }
    

    if you want to use only name.

    Login or Signup to reply.
  2. result class

    class Result {
      String? userName;
      int? hierarchyLevel;
      int? sequence;
      int? isSuccess;
    
      Result({this.userName, this.hierarchyLevel, this.sequence, this.isSuccess});
    
      Result.fromJson(Map<String, dynamic> json) {
        userName = json['userName'];
        hierarchyLevel = json['hierarchyLevel'];
        sequence = json['sequence'];
        isSuccess = json['isSuccess'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['userName'] = this.userName;
        data['hierarchyLevel'] = this.hierarchyLevel;
        data['sequence'] = this.sequence;
        data['isSuccess'] = this.isSuccess;
        return data;
      }
    }
    

    fix your response:

    final List<Result> listResult =
              (jsonData["result"] as List<dynamic>)
                  .map((e) => Result.fromJson(e as Map<String, dynamic>))
                  .toList();
    
    Login or Signup to reply.
  3. For parsing the json, you should use the Model in dart and using the helpers like fromJson toJson you should contact with the API.

    You can use https://javiercbk.github.io/json_to_dart/, which will create the Model for you , if you paste the json.

    For example consider using a Model.

    class Model {
      String? userName;
      int? hierarchyLevel;
      int? sequence;
      int? isSuccess;
    
      Model({this.userName, this.hierarchyLevel, this.sequence, this.isSuccess});
    
      Model.fromJson(Map<String, dynamic> json) {
        userName = json['userName'];
        hierarchyLevel = json['hierarchyLevel'];
        sequence = json['sequence'];
        isSuccess = json['isSuccess'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['userName'] = this.userName;
        data['hierarchyLevel'] = this.hierarchyLevel;
        data['sequence'] = this.sequence;
        data['isSuccess'] = this.isSuccess;
        return data;
      }
    }
    

    And use it as:

    final List<Result> result =   (jsonData["result"] as List<dynamic>)
                                   .map((item) => Model.fromJson(item as Map<String, dynamic>))
                                   .toList();
    

    Now you can access any of its child like:

    debugPrint(result[0].userName); // Raj
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search