首页 > 解决方案 > 如何从服务器解码此响应?我被困住了,响应的“数据”在地图中

问题描述

我来自服务器的 json 响应

{"success":1,"error":[],"data":{"38":{"address_id":"38","firstname":"Raj","lastname":"s","company":"","address_1":"aaaa","address_2":"","postcode":"966666","city":"aa","zone_id":"1234","zone":"Kerewan","zone_code":"KE","country_id":"0","country":"","iso_code_2":"","iso_code_3":"","address_format":"","custom_field":null},"37":{"address_id":"37","firstname":"Raj","lastname":"s","company":"","address_1":"4 kk\t","address_2":"","postcode":"56774\t","city":"Chennai\t","zone_id":"1234","zone":"Kerewan","zone_code":"KE","country_id":"0","country":"","iso_code_2":"","iso_code_3":"","address_format":"","custom_field":null},}}

我的最小代码

List<Address> listAddress;

     Future<List<Address>> getAddresList()async {
     List<Address> listAddress;
        {
       try {
      var response = await http.post(
          "URL",
          headers: {"content-type": "application/json", "cookie": cookie});
      List<Address> list = [];
      if (response.statusCode == 200) {
     var data=convert.jsonDecode(response.body);
        for (var item in convert.jsonDecode(response.body)) {
          list.add(AddressOpencart.fromJson(item) as Address);
          }
         }
       setState(() {
        listAddress = list;
        print("DDll"+listAddress.toString());
      });
    } catch (err,trace) {
      print(trace.toString());
      print(err.toString());
      rethrow;
    }
  }
}

我的地址模型

Address.fromOpencartJson(Map<String, dynamic> json) {
    try {
      firstName = json['firstname'];
      lastName = json['lastname'];
      street = json['address_1'];
      city = json['city'];
      state = json['zone'];
      country = json['country'];
      phoneNumber = json['phone'];
      zipCode = json['postcode'];
    } catch (e) {
      print(e.toString());
    }
}

标签: jsonflutterdart

解决方案


您必须在 API 响应中修复您的数据参数。它应该是一个对象数组。

{
  "success": 1,
  "error": [

  ],
  "data": [
     {
      "address_id": "38",
      "firstname": "Raj",
      "lastname": "s",
      "company": "",
      "address_1": "aaaa",
      "address_2": "",
      "postcode": "966666",
      "city": "aa",
      "zone_id": "1234",
      "zone": "Kerewan",
      "zone_code": "KE",
      "country_id": "0",
      "country": "",
      "iso_code_2": "",
      "iso_code_3": "",
      "address_format": "",
      "custom_field": null
    },
    {
      "address_id": "37",
      "firstname": "Raj",
      "lastname": "s",
      "company": "",
      "address_1": "4 kk\t",
      "address_2": "",
      "postcode": "56774\t",
      "city": "Chennai\t",
      "zone_id": "1234",
      "zone": "Kerewan",
      "zone_code": "KE",
      "country_id": "0",
      "country": "",
      "iso_code_2": "",
      "iso_code_3": "",
      "address_format": "",
      "custom_field": null
    }

  ]
}

现在,您的代码对我来说看起来不错,但几乎没有更改:

 Future<List<Address>> getAddresList()async {
   List<Address> listAddress;

   try {
        var response = await http.post(
            "URL",
            headers: {"content-type": "application/json", "cookie": cookie});
        List<Address> list = List<Address>();
        if (response.statusCode == 200) {
            var data = jsonDecode(response.body);
            for (var item in data["data"]) {
                list.add(AddressOpencart.fromJson(item) as Address);
            }
        }
        setState(() {
            listAddress = list;
            print("DDll"+listAddress.toString());
        });
  } catch (err,trace) {
    print(trace.toString());
    print(err.toString());    
  }
}

Address.fromOpencartJson(Map<String, dynamic> json) :
  firstName = json['firstname'],
  lastName = json['lastname'],
  street = json['address_1'],
  city = json['city'],
  state = json['zone'],
  country = json['country'],
  phoneNumber = json['phone'],
  zipCode = json['postcode'];

推荐阅读