flutter json解析

  • 导入库文件:
    import 'dart:convert' show json;
  • 数组解析:
    ['foo', { 'bar': 499 }]
Map data= json.decode(response);
data[1]['bar'];
  • 对象解析:
    {"current_user_url":"https://api.github.com/user"}
Map data= json.decode(response);
var url1= data['current_user_url'];

*实例:

getToken() async {
    Dio dio = new Dio();
    dio.options.baseUrl = ServerUrl.base;
    FormData formData = new FormData.from({
      "mobile": "13981981111",
      "pwd": "983532",
    });
    Response response = await dio.post(ServerUrl.token, data: formData);
    String result = "{\"status\": 0, \"description\": \"success\", \"data\": {\"token\": \"eyJhbGciOiJIUzI1NiJ9.eyJtZW1pZCI6IjEzOTgxOTgzNTMyIiwiY29tcGFueV9pZCI6MjAxNzExMjQzLCJleHAiOjE1NDUyODA5MjB9.3RnJR1i70jD1TUpDn52UTgOrqhhXRZpvS9yMMTD4G74\"}, \"extra\": {\"ssize\": 1, \"snow\": 1542688920, \"other\": null}}";//response.data.toString();
    print(result);
    Map resultMap = json.decode(result);
    String token = resultMap["data"]["token"];
    print(token);
  }

你可能感兴趣的:(flutter json解析)