【问题标题】:Parse nested json failed flutter解析嵌套的 json 失败颤振
【发布时间】:2021-03-29 11:35:15
【问题描述】:

我有这个 json 响应,其中包含服务类型、车辆型号、附件、工作状态作为数组,但我无法访问任何类,当我打印 print(obj); 时它只显示 null

Json 文件

{
  "success": true,
  "message": "",
  "data": {
    "service_types": [
      {
        "id": 1,
        "name": "Car Wash"
      },
      {
        "id": 2,
        "name": "Full Body Wash"
      },
      {
        "id": 3,
        "name": "Polishing"
      },
      {
        "id": 4,
        "name": "Engine Oil Checkup"
      },
     
    ],
    "vehicle_models": [
      {
        "id": 1,
        "name": "SUV"
      }
    ],
    "accessories": [
      {
        "id": 2,
        "name": "Mat Cover"
      },
      {
        "id": 4,
        "name": "Cash"
      },
      {
        "id": 5,
        "name": "Engine Room washing"
      }
    ],
    "working_statuses": [
      {
        "id": 0,
        "name": "Registered"
      },
      {
        "id": 1,
        "name": "Work started"
      },
      {
        "id": 2,
        "name": "Work completed"
      },
      {
        "id": 5,
        "name": "Cancelled"
      }
    ]
  }
}

数据提取代码

var response = await http.post(Uri.parse(Urls.DasboardData),
    headers: {"Content-Type": "application/json"},
    body: json.encode({
      "vehicle_models": "",
      "service_types": "",
      "station_id":Station_id,
      "accessories": "",
      "working_statuses": "",
    }));

Map<String, dynamic> value = json.decode(response.body);

var data = value['data'];
if (data.length > 0) {
  
    for (int i = 0; i < data.length; i++) {
    var obj = data[i];
     print(obj);
    var service_type_obj = obj['service_types'];
    var vehicle_models_obj = obj['vehicle_models'];
    var accessories_obj = obj['accessories'];
    var working_statuses_obj = obj['working_statuses'];
    DataList.add(GetAllModel(
      service_type_obj['id'],
      service_type_obj['name'],
      vehicle_models_obj['id'],
      vehicle_models_obj['name'],
      accessories_obj['id'],
      accessories_obj['name'],
      working_statuses_obj['id'],
      working_statuses_obj['name'],
    ));
  }

  setState(() {
    print("UI UPDATED");
  });
}
else
  {

  } 

型号

class GetAllModel {
  String service_type_id,
        service_type_name,
        vehicle_models_id,
        vehicle_models_name,
        accessories_id,
        accessories_name,
        working_statuses_id,
       working_statuses_name;


  GetAllModel(
      this.service_type_id,
      this.service_type_name,
      this.vehicle_models_id,
      this.vehicle_models_name,
      this.accessories_id,
      this.accessories_name,
      this.working_statuses_id,
      this.working_statuses_name,
      );
}

【问题讨论】:

  • 在输入 if 条件之前打印变量时,它显示什么?
  • 我可以打印 data 的长度和响应,但是当它进入 for 循环时它显示 null @FelipeVergara

标签: flutter dart flutter-layout


【解决方案1】:

问题是在迭代你的信息时,数据大小是4,但是这个它包含4个对象,因此你将无法访问data[i],因为data[i]不存在,如果它存在数据 ['service_types'] 这是一个列表,如果您可以迭代包含对象的信息,请在此处。

因此,为了能够访问对象的信息,您必须像这样迭代它们:

for (var i = 0; i < data['service_types'].length; i++) {
   print(data['service_types'][i]);
}

其他对象也一样。

迭代的替代方法:

for (var item in data.values) {
   print(item);
}

【讨论】:

  • 所以我必须做这所有 4 个对象,有没有更好的方法@Felipe Vergara
  • 这样你可以迭代所有的信息,它打印每个对象的信息。 for (var item in data.values) { print(item); }
  • 你能展示一些代码示例吗@Felipe Vergara
  • 我如何访问数据然后发送到模型,我该怎么做@Felipe Vergara
  • 您的模型不理解,根据您今天收到的信息,有些字段将为空,这是您期望的行为吗?
【解决方案2】:

你需要改变这些:

var service_type_obj = obj['service_types'];
var vehicle_models_obj = obj['vehicle_models'];
var accessories_obj = obj['accessories'];
var working_statuses_obj = obj['working_statuses'];

进入这个:

var service_type_obj = obj['service_types'][0];
var vehicle_models_obj = obj['vehicle_models'][0];
var accessories_obj = obj['accessories'][0];
var working_statuses_obj = obj['working_statuses'][0];

您的对象位于列表中,要访问它们,您必须使用索引,例如[0]。

【讨论】:

  • 我也试过了,当我 print(obj) 它显示 null 时,它会停在 var service_type_obj = obj['service_types']; 即使我添加 var service_type_obj = obj['service_types'][0]; 它仍然卡在 @Huthaifa Muayyad
猜你喜欢
  • 2020-11-15
  • 2023-03-22
  • 2023-01-14
  • 2021-05-02
  • 2019-01-17
  • 2021-10-27
  • 1970-01-01
  • 2019-02-22
  • 2020-08-10
相关资源
最近更新 更多