Answers for "flutter json to list object"

5

flutter json to list

List to JSON: 
_______________________________________________________________________________
List<String> fruits = ['Apple', 'Banana', 'Orange', 'Strawberry'];
String jsonFruits = jsonEncode(fruits);

________________________________________________________________________________
JSON to List:
________________________________________________________________________________
You need to convert each item individually

	var json = jsonEncode(jsonFruits.map((e) => e.toJson()).toList());

or pass an toEncodable function

	var json = jsonEncode(jsonFruits, toEncodable: (e) => e.toJsonAttr());
Posted by: Guest on May-20-2021
5

dart list to json

import 'dart:convert';

main() {
  List<String> tags = ['tagA', 'tagB', 'tagC'];
  String jsonTags = jsonEncode(tags);
  print(jsonTags);      // ["tagA","tagB","tagC"]
}
Posted by: Guest on May-19-2021
0

json to list flutter

List<UserModel> users = (json.decode(response.body) as List)
      .map((data) => UserModel.fromJson(data))
      .toList();
Posted by: Guest on July-28-2021
0

how to parse an array of json objects in flutter

//pass your decoded json
Map<String, dynamic> json = jsonDecode(rawJson);

List<Location> locations = List<Location>.from(json["locations"].map((x) => Location.fromJson(x)))
//Location class => days is a premitive list and can be deserialized below
class Location {
  Location({
    this.name,
    this.days,
  });

  String name;
  List<int> days;

  factory Location.fromJson(Map<String, dynamic> json) => Location(
    name: json["name"],
    days: List<int>.from(json["days"].map((x) => x)),
  );

  Map<String, dynamic> toJson() => {
    "name": name,
    "days": List<dynamic>.from(days.map((x) => x)),
  };
}
Posted by: Guest on August-29-2020

Code answers related to "Dart"

Browse Popular Code Answers by Language