flutter json解析

2020-09-09  本文已影响0人  喜剧收尾_XWX

一、在线json转dart

https://javiercbk.github.io/json_to_dart/

二、使用 json_serializable解析

  1. pubspec.yaml中引入框架
dependencies:
  flutter:
    sdk: flutter
  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.3
  json_annotation: ^3.0.1

dev_dependencies:
  flutter_test:
    sdk: flutter

  build_runner: ^1.7.2
  json_serializable: ^3.2.5
  1. 使用https://caijinglong.github.io/json2dart/index_ch.html转换成model
    小工具

3.在根目录运行

4.使用

import 'dart:convert';
Map userMap = JSON.decode(json);
var user = new User.fromJson(userMap);
String json = JSON.encode(user);
上一篇下一篇

猜你喜欢

热点阅读