JSON 解析和複雜數據模型轉換

一、後臺返回以下數據,要將records裏的每一條數據顯示在界面。git

步驟1、複製要解析的數據github

步驟2、到https://javiercbk.github.io/json_to_dart/解析,修改類名,而後就能夠用到項目中啦json

步驟3、引用數據模型並使用後端

/// 獲取服務頁面數據
Future<ServiceModel> getServicePageConten(formPage) async {
  try {
    print('開始獲取數據...............');
    Dio dio = new Dio();
    // 後臺返回數據
    var response = await dio.get(_API_GET_PAGE, queryParameters: formPage);
    // 將後臺對象轉爲json
    var  data = json.decode(response.toString());
    //  解析模型只是data裏的數據,這裏也只要data,要對應
    ServiceModel serviceList=  ServiceModel.fromJson(data['data']);
    if (response.statusCode == 200) {
        serviceList.records.forEach((item)=>print(item.productName));
        return serviceList;
    } else {
      throw Exception('後端接口出現異常,請檢測代碼和服務器狀況.........');
    }
  } catch (e) {
      print('ERROR:======>${e}');
  }
}
相關文章
相關標籤/搜索