I make an API from Laravel and check the 'Get' request from Postman where it will return as follow:
"answer_selected": "{1:\"True\",2:\"False\"}"
Then, the Flutter application will read the JSON and serialize it using the model class.
Are there any ways to convert the value of "answer_selected" to map<int, dynamic>? Or, my JSON API response format is incorrect?
You should not return a string that is not a complete JSON. Either return
or
will be much better. jsonDecode in dart supports decode to map<String, dynamic, and the key in JSON must be string too. So the JSON should like
If you want to transform it to map<int, dynamic>, just do
void test() {
String text = '{"1":"True","2":"False"}';
Map<String, dynamic> map = jsonDecode(text);
Map<int, dynamic> desiredMap =
map.map((key, value) => MapEntry(int.parse(key), value));
desiredMap.entries.forEach((element) {
print('${element.key} ${element.value}');
});
}