flutter dio解析Json的两种方式类型转换

void getHttp() async {
  var url = 'https://**********************/AppAccess/GetRestaurantCategoryList';
  try {
    var response = await Dio().get(url);
       if (response.statusCode == HttpStatus.ok) {
        var json = jsonDecode(response.data);
        for (var item in json["data"]) {
          var model = FoodItem.fromJson(item);
          print(model.categoryId);
          print(model.categoryName);
        }
  } catch (e) {
    print(e);
  }

注意上面是直接Json转换成实体,下面是根据json中的键值对,取值。
带有async的方法返回值必须是Future

Future getHomePageContent() async {
 
  try {
    Response response;
    Dio dio = new Dio();
    response = await dio.get("https://**********************/AppAccess/GetRestaurantCategoryList");
    if (response.statusCode == HttpStatus.ok) {
      var resdata= jsonDecode(response.data);
      for (var item in resdata["data"]) {
          FoodItem model =new FoodItem();
          model.categoryId=item["CategoryId"];
          model.categoryName=item["CategoryName"];
          print(model.categoryId);
          print(model.categoryName);
        }
      return response.data;
    } else {
      throw Exception("...-");
    }
  } catch (e) {
    return print("error:" + e.toString());
  }
}

实体 (https://javiercbk.github.io/json_to_dart/ 可以自动的把json生产model)

class FoodItem {
  int? categoryId;
  String? categoryName;
  String? createdBy;
  String? createdDateTime;
  String? updatedBy;
  String? updatedDateTime;
  String? isDel;

  FoodItem(
      {this.categoryId,
      this.categoryName,
      this.createdBy,
      this.createdDateTime,
      this.updatedBy,
      this.updatedDateTime,
      this.isDel});

  FoodItem.fromJson(Map<String, dynamic> json) {
    categoryId = json['CategoryId'];
    categoryName = json['CategoryName'];
    createdBy = json['CreatedBy'];
    createdDateTime = json['CreatedDateTime'];
    updatedBy = json['UpdatedBy'];
    updatedDateTime = json['UpdatedDateTime'];
    isDel = json['IsDel'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['CategoryId'] = this.categoryId;
    data['CategoryName'] = this.categoryName;
    data['CreatedBy'] = this.createdBy;
    data['CreatedDateTime'] = this.createdDateTime;
    data['UpdatedBy'] = this.updatedBy;
    data['UpdatedDateTime'] = this.updatedDateTime;
    data['IsDel'] = this.isDel;
    return data;
  }
}

json

{
    "data": [
        {
            "CategoryId": 17,
            "CategoryName": "Coffee",
            "CreatedBy": null,
            "CreatedDateTime": null,
            "UpdatedBy": null,
            "UpdatedDateTime": null,
            "IsDel": null
        },
        {
            "CategoryId": 18,
            "CategoryName": "Fruit",
            "CreatedBy": null,
            "CreatedDateTime": null,
            "UpdatedBy": null,
            "UpdatedDateTime": null,
            "IsDel": null
        }
    ]
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值