dart JSON String convert to List String

Convert Json Data To List

List<String> data = [
   "006.01.01",
   "006.01.01 1090",
   "006.01.01 1090 1090.950",
   "006.01.01 1090 1090.950 052",
   "006.01.01 1090 1090.950 052 A",
   "006.01.01 1090 1090.950 052 A 521219",
   "006.01.01 1090 1090.950 052 A 521219",
   "006.01.01 1090 1090.950 052 A 521219",
   "006.01.01 1090 1090.950 052 A 521219",
   "006.01.01 1090 1090.950 052 A 521219",
   "006.01.01 1090 1090.950 052 B",
   "006.01.01 1090 1090.950 052 B 521211",
   "006.01.01 1090 1090.950 052 B 521211",
   "006.01.01 1090 1090.994",
   "006.01.01 1090 1090.994 001",
   "006.01.01 1090 1090.994 001 A",
   "006.01.01 1090 1090.994 001 A 511111",
   "006.01.01 1090 1090.994 001 A 511111",
   "006.01.01 1090 1090.994 001 A 511111",
   "006.01.01 1090 1090.994 001 A 511111"
];

//your json string
String jsonString = json.encode(data);

//convert json string to list
List<String> newData = List<String>.from(json.decode(jsonString));

Try this one. Hope it helps.

import 'dart:convert';

void main() {
  String jsonResponse = '''
    ["006.01.01",
    "006.01.01 1090",
    "006.01.01 1090 1090.950",
    "006.01.01 1090 1090.950 052",
    "006.01.01 1090 1090.950 052 A",
    "006.01.01 1090 1090.950 052 A 521219",
    "006.01.01 1090 1090.950 052 A 521219",
    "006.01.01 1090 1090.950 052 A 521219",
    "006.01.01 1090 1090.950 052 A 521219",
    "006.01.01 1090 1090.950 052 A 521219",
    "006.01.01 1090 1090.950 052 B",
    "006.01.01 1090 1090.950 052 B 521211",
    "006.01.01 1090 1090.950 052 B 521211",
    "006.01.01 1090 1090.994",
    "006.01.01 1090 1090.994 001",
    "006.01.01 1090 1090.994 001 A",
    "006.01.01 1090 1090.994 001 A 511111",
    "006.01.01 1090 1090.994 001 A 511111",
    "006.01.01 1090 1090.994 001 A 511111",
    "006.01.01 1090 1090.994 001 A 511111"]
  ''';

  dynamic jsonParsed = json.decode(jsonResponse);

//   print(jsonParsed);

  print(jsonParsed[5]);
}

The result of parsing a JSON list is a List<dynamic>. The return type of jsonDecode is just dynamic.

You can cast such a list to a List<String> as

List<String> stringList = (jsonDecode(input) as List<dynamic>).cast<String>();

You can also just use it as a List<dynamic> and then assign each value to String:

List<dynamic> rellyAStringList = jsonDecode(input);
for (String string in reallyAStringList) { ... }

The effect is approximately the same - each element is checked for being a string when it is taken out of the list.

Tags:

Dart

Flutter