在Flutter / Dart中解析列表

时间:2019-06-12 10:31:11

标签: json flutter dart

我有一个看起来像这样的列表:

[{id: 1, user_id: 3, challenge_id: 1, created_at: 2019-06-09 06:36:39, image_caption: Enter your image caption here, image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560062199/zex61jegvqwkevq6qrmd.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0}, {id: 2, user_id: 2, challenge_id: 1, created_at: 2019-06-12 09:17:07, image_caption: , image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560331027/dj94sjzufx8gznyxrves.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0}, 
{id: 2, user_id: 3, challenge_id: 1, created_at: 2019-06-09 06:36:39, image_caption: Enter your image caption here, image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560062199/zex61jegvqwkevq6qrmd.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0}, {id: 2, user_id: 2, challenge_id: 1, created_at: 2019-06-12 09:17:07, image_caption: , image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560331027/dj94sjzufx8gznyxrves.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0}, 
{id: 3, user_id: 3, challenge_id: 1, created_at: 2019-06-09 06:36:39, image_caption: Enter your image caption here, image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560062199/zex61jegvqwkevq6qrmd.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0}, {id: 2, user_id: 2, challenge_id: 1, created_at: 2019-06-12 09:17:07, image_caption: , image_path: https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560331027/dj94sjzufx8gznyxrves.jpg, image: null, user_upvoted: null, user_downvoted: null, score: 0}]

所以基本上是3个对象。

对象模型如下:

class ChallengeUpload {
  int id = 0;
  int userId = 0;
  int challengeId = 0;
  String createdAt = "";
  String imageCaption = "";
  String imagePath = "";
  File image;
  String userUpvoted = "";
  String userDownvoted = "";
  int score = 0;

  ChallengeUpload();

  ChallengeUpload.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    userId = json['user_id'];
    challengeId = json['challenge_id'];
    createdAt = json['created_at'];
    imageCaption = json['image_caption'];
    imagePath = json['image_path'];
    userUpvoted = json['user_upvoted'];
    userDownvoted = json['user_downvoted'];
    score = json['score'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['user_id'] = this.userId;
    data['challenge_id'] = this.challengeId;
    data['created_at'] = this.createdAt;
    data['image_caption'] = this.imageCaption;
    data['image_path'] = this.imagePath;
    data['user_upvoted'] = this.userUpvoted;
    data['user_downvoted'] = this.userDownvoted;
    data['score'] = this.score;
    return data;
  }
}

我似乎无法弄清楚如何解析此列表。

如果我的API返回的只是一个对象,则可以通过以下函数运行它:

currentChallenge = Challenge.fromJson(response.data);

如何执行类似的操作,但最终得到一个UploadedChallengesList而不是单个对象?

我尝试过:

challengeUploads = json.decode(response.data).map<ChallengeUpload>((dynamic challengeUpload) => ChallengeUpload.fromJson(challengeUpload)).toList();

它将打印: I/flutter ( 2660): type 'List<dynamic>' is not a subtype of type 'String'

我的JSON:

{  
   id:1,
   user_id:3,
   challenge_id:1,
   created_at:2019-06   -09   06:36:39,
   image_caption:Enter your image caption here,
   image_path:   https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560062199/zex61jegvqwkevq6qrmd.jpg,
   image:null,
   user_upvoted:null,
   user_downvoted:null,
   score:0
},
{  
   id:2,
   user_id:2,
   challenge_id:1,
   created_at:2019-06   -12   09:17:07,
   image_caption:,
   image_path:   https://res.cloudinary.com/dqrmgdpcf/image/upload/v1560331027/dj94sjzufx8gznyxrves.jpg,
   image:null,
   user_upvoted:null,
   user_downvoted:null,
   score:0
}

编辑:我发现它与我用来进行API调用的库有关。我使用Dio,我的代码最终看起来像这样:

  Response response = await Dio().get(url,
      options: Options(
          headers: {"Authorization": accessToken},
          responseType: ResponseType.json));

  setState(() {
    challengeUploads = response.data
        .map<ChallengeUpload>((dynamic challengeUpload) =>
            ChallengeUpload.fromJson(challengeUpload))
        .toList();
  });

1 个答案:

答案 0 :(得分:1)

怎么样?

List<Map<String, dynamic>> jsonList = json.decode(response.data.toString()) as List;

List<ChallengeUpload> myList = jsonList.map(
    (jsonElement) => ChallengeUpload.fromJson(jsonElement)
).toList();