31

I have an API that calls the json String array as follows:

[
  "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"
]

I intend to convert the json to the List in the dart. I tried the script below :

json.decode(response.body).cast<List<String>();

but it didn't work, how should the script be correct?

5 Answers 5

63

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.

Sign up to request clarification or add additional context in comments.

Comments

17

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));

Comments

1

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]);
}

Comments

1

If the outermost data is an object and you want to list it inside you can use the following:

    var json = '{"id":1,"tags":["a","b","c"]}';
    var data = jsonDecode(json);

    List<dynamic> rawTags = data['tags'];

    List<String> tags = rawTags.map(
      (item) {
        return item as String;
      },
    ).toList();

    print(tags);

Comments

0

A possibility to convert any dynamic Json to List of Strings maybe this approach:

List<String> _data = []; // List to store the result
const int MAXL = 40; // max length lines
String line = ''; // A single line
int i = 0; // To identify level recursion

void makeArrayList (Map ret) {

  // function to call at any level of recursion
  void CheckKeyValue (Map data) {
    i++;

    data.forEach((key, value) {
      if (value is Map) {
        _data.add('** $key *$i*');
        CheckKeyValue (value);
        i--;
      } else {
        if (value is List) {
          for (var el in value) {
            if (el is Map) {
              CheckKeyValue (el);
              i--;
            } else {
              line = '$key: $value'.length > MAXL ?
                     line.substring(0, MAXL) : '$key: $value';

              _data.add(line);
            }
            _data.add(''); // \n line
          }
        } else {
          line = '$key: $value'.length > MAXL ?
                 line.substring(0, MAXL) : '$key: $value';

          _data.add(line);
        }
      }
    });
  }

  CheckKeyValue (ret);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.