【问题标题】:JSON data parsing in FlutterFlutter 中的 JSON 数据解析
【发布时间】:2021-10-21 14:38:31
【问题描述】:

我有来自 API 的 JSON 数据,我想按类解析它。但我有一个问题。在第一个视频中,questions 等于 null,但在另一个视频中,它不是 null。我不明白如何解析它。

[{type: MAIN, video: {id: firstIdStr, url: firstURLStr, thumbnail: firstThumbmainURLStr, duration: 4363}, question: null}, {type: ANSWER_FOR_INTERVIEW, video: {id: secondIdStr, url: secondURLStr, thumbnail: secondThumbmainURLStr, duration: 4123}, question: {id:quiestioId, category:questionCategory, text:questionText}, {type: ANSWER_FOR_INTERVIEW, video: {id: thirdIdStr, url: thirdURLStr, thumbnail: thirdThumbmainURLStr, duration: 4123}, question: {id:quiestioId, category:questionCategory, text:questionText}}]

【问题讨论】:

    标签: json flutter dart mobile mobile-application


    【解决方案1】:

    我终于找到答案了

    class Question {
      Question({
        this.category,
        this.id,
        this.text,
      });
    
      String category;
      String id;
      String text;
    
      factory Question.fromJson(Map<String, dynamic> json) => json == null
          ? null
          : Question(
              category: json["category"],
              id: json["id"],
              text: json["text"],
            );
    
      Map<String, dynamic> toJson() => {
            "category": category != null ? category : "",
            "id": id != null ? id : "",
            "text": text != null ? text : "",
          };
    }
    

    【讨论】:

      猜你喜欢
      • 2020-01-07
      • 2020-10-13
      • 1970-01-01
      • 2020-08-21
      • 2020-07-13
      • 2020-06-26
      • 2021-01-31
      • 2021-01-03
      • 2020-08-09
      相关资源
      最近更新 更多