Home > Software engineering >  Flutter converting json value to List<List<double>
Flutter converting json value to List<List<double>

Time:10-24

My value from json : "[[2.1,2.2],[1.0,2.5]]". How to convert this string to List<List> ?

CodePudding user response:

You start off with a List<dynamic> so you can just cast each of its elements to List<double>.

void main() {
  final decoded = json.decode('[[2.1,2.2],[1.0,2.5]]') as List<dynamic>;
  final listDoubleDouble =
      decoded.map<List<double>>((e) => e.cast<double>()).toList();
  print(listDoubleDouble.runtimeType);
}

CodePudding user response:

String myJSON = '[[2.1,2.2],[1.0,2.5]]';
var json = jsonDecode(myJSON);
print(json[0][0]);
  • Related