【问题标题】:How to get last object in json array and insert to new array如何获取json数组中的最后一个对象并插入新数组
【发布时间】:2020-07-09 08:00:52
【问题描述】:

我正在开发一个颤振应用程序,我有一个来自 API 的 JSON 数组,它包含列表数组中的对象,所以我需要获取数组中的最后一个对象并将最后一个对象推送到新数组因为我需要返回它将数组列表放入列表视图以显示...我该怎么做

我的代码

main() {
  var  data = json.decode('[ {"id":42,"image_path":"https://fathomless-brushlands-95996.herokuapp.com/Imaga_halls/1583971227.jpg","hall_details":"*********"},
    {"id":52,"image_path":"https://fathomless-brushlands-95996.herokuapp.com/Imaga_halls/1584390666.jpg",
    "hall_details":" Could anyone please help me with this. I am stuck here. Have been trying different methods but none working. Thank you"},
    {"id":62,
    "image_path":"https://fathomless-brushlands-95996.herokuapp.com/Imaga_halls/1584453580.jpg",
    "hall_details":"Could anyone please help me with this. I am stuck here. Have been trying different methods but none working. Thank you."},
    {"id":65,
    "image_path":"https://fathomless-brushlands-95996.herokuapp.com/Imaga_halls/1584453580.jpg",
    "hall_details":"Could anyone please help me with this. I am stuck here. Have been trying different methods but none working. Thank you."}]');

  Map responseJson = data[data.length-1];
      
  List returnMovies = [];
     
  returnMovies.addAll({responseJson});
            
  print(returnMovies);
}          

【问题讨论】:

    标签: arrays json flutter


    【解决方案1】:

    通用答案是使用这个库https://github.com/k-paxian/dart-json-mapper

    它不仅可以帮助您解决这种情况,还可以帮助您解决所有 Dart Object => JSON => Dart Object 的情况。

    请先仔细阅读库自述文件,尤其是“基本设置”部分。

    以下代码将帮助您掌握主要思想:

    import 'main.reflectable.dart' show initializeReflectable;
    import 'package:dart_json_mapper/dart_json_mapper.dart' show jsonSerializable, JsonMapperAdapter, typeOf, JsonMapper, DeserializationOptions, CaseStyle;
    
    @jsonSerializable
    class Movie {
      int id;
      String imagePath;
      String hallDetails;
    }
    
    void main {
          initializeReflectable();
    
          // given
          final json =
              '''[{"id":42,"image_path":"https://fathomless-brushlands-95996.herokuapp.com/Imaga_halls/1583971227.jpg","hall_details":"*********"},
          {"id":52,"image_path":"https://fathomless-brushlands-95996.herokuapp.com/Imaga_halls/1584390666.jpg",
          "hall_details":" Could anyone please help me with this. I am stuck here. Have been trying different methods but none working. Thank you"},
          {"id":62,
          "image_path":"https://fathomless-brushlands-95996.herokuapp.com/Imaga_halls/1584453580.jpg",
          "hall_details":"Could anyone please help me with this. I am stuck here. Have been trying different methods but none working. Thank you."},
          {"id":65,
          "image_path":"https://fathomless-brushlands-95996.herokuapp.com/Imaga_halls/1584453580.jpg",
          "hall_details":"Could anyone please help me with this. I am stuck here. Have been trying different methods but none working. Thank you."}]''';
    
          JsonMapper().useAdapter(JsonMapperAdapter(valueDecorators: {
            typeOf<List<Movie>>(): (value) => value.cast<Movie>()
          }));
    
          // when
          final moviesList = JsonMapper.deserialize<List<Movie>>(
              json, DeserializationOptions(caseStyle: CaseStyle.Snake));
    
          // then
          expect(moviesList.last, TypeMatcher<Movie>());
    
          print(moviesList.last.id);
          print(moviesList.last.hallDetails);
          print(moviesList.last.imagePath);
    }
    

    【讨论】:

    • 非常感谢兄弟
    猜你喜欢
    • 2021-12-26
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    相关资源
    最近更新 更多