29

How to encode list to json?

This is my class for Json.

class Players{
  List<Player> players;

  Players({this.players});

  factory Players.fromJson(List<dynamic> parsedJson){

    List<Player> players = List<Player>();
    players = parsedJson.map((i)=>Player.fromJson(i)).toList();

    return Players(
      players: players,
    );
  }
}

class Player{
  final String name;
  final String imagePath;
  final int totalGames;
  final int points;

  Player({this.name,this.imagePath, this.totalGames, this.points});

  factory Player.fromJson(Map<String, dynamic> json){

    return Player(
      name: json['name'],
      imagePath: json['imagePath'],
      totalGames: json['totalGames'],
      points: json['points'],
    );
  }
}

I managed to decode with fromJson, the result is in List. Now that I have another player to add in json and want to encode the list to json, have no idea to do it. It result always failed.

var json = jsonDecode(data);
List<Player> players = Players.fromJson(json).players;
Player newPlayer = Player(name: _textEditing.text,imagePath: _imagePath,totalGames: 0,points: 0);
players.add(newPlayer);
String encode = jsonEncode(players.players);

What do I need to add on Players or Player?

1
  • Your code is incorrect. The players variable is of type List. Accordingly, this code players.players will not work, because List does not have a players field. Commented Jun 5, 2019 at 8:46

3 Answers 3

41

Add toJson method to your Player class:

Map<String, dynamic> toJson(){
  return {
    "name": this.name,
    "imagePath": this.imagePath,
    "totalGames": this.totalGames,
    "points": this.points
  };
}

Then you can call jsonEncode on the list of players:

String encoded = jsonEncode(players) // this will automatically call toJson on each player
Sign up to request clarification or add additional context in comments.

2 Comments

You don't need the static function. jsonEncode will work with a List<Player> if the class has toJson implemented.
@Lucas You are right! No need for static function. Thank you!
17

Add on class:

Map<String,dynamic> toJson(){
    return {
        "name": this.name,
        "imagePath": this.imagePath,
        "totalGames": this.totalGames,
        "points": this.points
    };
  }

and call

String json = jsonEncode(players.map((i) => i.toJson()).toList()).toString();

2 Comments

Can you explain why is variable json declared as String? I'm quite confused
Hi Steven, sometimes we need to work with json as plain/text, but it's just an example.
7
List jsonList = players.map((player) => player.toJson()).toList();
print("jsonList: ${jsonList}");

1 Comment

Can you please add description on why this solves the issue mentioned in the question ?

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.