Parse JSON Array without key using Dart

2019-08-26 20:18发布

问题:

I can't seems to find a solution to parse a json array without key using Dart language. All i can find is by using Java. I need to parse something like this..

[
 5,
 10,
 15,
 20
]

Java solution is from here

Please inform me if I have duplicate question. Thank you!

回答1:

Just use json.decode as normal, for example:

List<dynamic> l = json.decode('[5, 10, 15, 20]');

It happens that the members of l will all be ints



回答2:

import 'dart:convert';
void main() {
  var x = "[ 5, \n 10, \n15\n, 20]";
  var b = jsonDecode(x);
  print(b[3]); //prints 20

}
  • DartPad Example
  • Official docs