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
This error is simply says that you are trying to convert Map into String.
now ‘listValJson’ is
List<Map<String,dynamic>>
notList<String>
Try to create a data model for all user’s data or use
if you want to use only name.
result class
fix your response:
For parsing the
json
, you should use theModel
indart
and using the helpers likefromJson
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 thejson
.For example consider using a
Model
.And use it as:
Now you can access any of its child like: