Можно ли как-то разобрать массив ответов JSON с декодером и добавить его в массив? - PullRequest
0 голосов
/ 26 июня 2019

Это мой ответ JSON:

{
"0": 200,
"success": true,
"response": [
    {
        "id": 89,
        "fkuser_id": 17,
        "assessee_year_from": "2019",
        "assessee_year_to": "2020",
        "section_822BB": "n",
        "assessee_name": ";;koljkikju",
        "gender": "m",
        "tin": "21313123123123",
        "old_tin": "411211233213123",
        "circle": "joji",
        "zone": "25321321",
        "resident_status": "r",
        "govt_employee_status": "n",
        "person_category": "10b",
        "date_of_birth": "0000-00-00",
        "income_year_from": "2018",
        "income_year_to": "2019",
        "employer_name": "3213123",
        "spouse_name": "123123",
        "spouse_tin": "13132313132123",
        "fathers_name": "31313",
        "mothers_name": "131313",
        "present_address": "131313",
        "permanent_address": "131313",
        "city_corporation": "Other City Corporation",
        "contact": "31313",
        "email": "skfuadsakib@gmail.com",
        "nid": "31",
        "bid": "31313",
        "signature": "2019-06-26 065446.jpg",
        "date_of_signature": "0000-00-00",
        "place_of_signature": null,
        "order_id": 138,
        "agent_id": null,
        "created_at": "2019-06-26 06:54:46",
        "updated_at": "2019-06-26 06:55:06"
    },
    {
        "id": 62,
        "fkuser_id": 17,
        "assessee_year_from": "2019",
        "assessee_year_to": "2020",
        "section_822BB": "y",
        "assessee_name": "David Herrera",
        "gender": "m",
        "tin": "654131651651",
        "old_tin": "6514616416",
        "circle": "sdffsdajklfsdkf",
        "zone": "Dhanmondi",
        "resident_status": "r",
        "govt_employee_status": "y",
        "person_category": "10b",
        "date_of_birth": "2019-06-24",
        "income_year_from": "2018",
        "income_year_to": "2019",
        "employer_name": "sadfasdfasdfas",
        "spouse_name": "sadfsadf",
        "spouse_tin": "asdfas",
        "fathers_name": "sadfasd",
        "mothers_name": "sadfasdf",
        "present_address": "6741 Sylmar Ave",
        "permanent_address": "6741 Sylmar Ave",
        "city_corporation": "Other City Corporation",
        "contact": "2818081090",
        "email": "mislam629@gmail.com",
        "nid": "51165164196",
        "bid": "65416516516",
        "signature": "",
        "date_of_signature": "2019-06-24",
        "place_of_signature": "2",
        "order_id": 106,
        "agent_id": null,
        "created_at": "2019-06-24 11:06:44",
        "updated_at": "2019-06-24 11:07:10"
    },
    {
        "id": 59,
        "fkuser_id": 17,
        "assessee_year_from": "2019",
        "assessee_year_to": "2020",
        "section_822BB": "y",
        "assessee_name": "Shofique Shahariar",
        "gender": "m",
        "tin": "651616326552416",
        "old_tin": "545621523485",
        "circle": "sdffsdajklfsdkf",
        "zone": "Dhanmondi",
        "resident_status": "r",
        "govt_employee_status": "y",
        "person_category": "10a",
        "date_of_birth": "2019-06-24",
        "income_year_from": "2018",
        "income_year_to": "2019",
        "employer_name": "asdfasdfasf asdfa ssd f",
        "spouse_name": "sad asdf asfasdfasdf",
        "spouse_tin": "asdf asdfasdf",
        "fathers_name": "a fasfas fas",
        "mothers_name": "as fasdf a",
        "present_address": "Khallanpur Dhaka",
        "permanent_address": "Khallanpur Dhaka",
        "city_corporation": "Dhaka/Chittagong City Corporation",
        "contact": "2818081090",
        "email": "mislam629@gmail.com",
        "nid": null,
        "bid": "5645645645",
        "signature": "",
        "date_of_signature": "2019-06-24",
        "place_of_signature": "3",
        "order_id": null,
        "agent_id": null,
        "created_at": "2019-06-24 06:53:45",
        "updated_at": "2019-06-24 06:53:45"
    },
    {
        "id": 46,
        "fkuser_id": 17,
        "assessee_year_from": "2001",
        "assessee_year_to": "2002",
        "section_822BB": "y",
        "assessee_name": "Ashiq",
        "gender": "m",
        "tin": "123456789876",
        "old_tin": "6456456",
        "circle": "4565645",
        "zone": "dfgdfgd",
        "resident_status": "r",
        "govt_employee_status": "y",
        "person_category": "10a",
        "date_of_birth": "2019-12-14",
        "income_year_from": "2001",
        "income_year_to": "2002",
        "employer_name": "fsfsdf",
        "spouse_name": "fgdfg",
        "spouse_tin": "5464545",
        "fathers_name": "uytyutyu",
        "mothers_name": "tutyutyu",
        "present_address": "tyutyu",
        "permanent_address": "tyutyutyu",
        "city_corporation": "yutyutyuty",
        "contact": "7567567",
        "email": "utyu@tr.com",
        "nid": "564564565",
        "bid": "564565",
        "signature": "2019-06-18-5d08556321bf6.png",
        "date_of_signature": "2019-12-14",
        "place_of_signature": "1",
        "order_id": 100,
        "agent_id": null,
        "created_at": "2019-06-18 09:07:15",
        "updated_at": "2019-06-21 13:14:49"
    },
    {
        "id": 36,
        "fkuser_id": 17,
        "assessee_year_from": "2001",
        "assessee_year_to": "2002",
        "section_822BB": "y",
        "assessee_name": "Ashiq",
        "gender": "m",
        "tin": "123456789876",
        "old_tin": "6456456",
        "circle": "4565645",
        "zone": "dfgdfgd",
        "resident_status": "r",
        "govt_employee_status": "y",
        "person_category": "10a",
        "date_of_birth": "2019-12-14",
        "income_year_from": "2001",
        "income_year_to": "2002",
        "employer_name": "fsfsdf",
        "spouse_name": "fgdfg",
        "spouse_tin": "5464545",
        "fathers_name": "uytyutyu",
        "mothers_name": "tutyutyu",
        "present_address": "tyutyu",
        "permanent_address": "tyutyutyu",
        "city_corporation": "yutyutyuty",
        "contact": "7567567",
        "email": "utyu@tr.com",
        "nid": "564564565",
        "bid": "564565",
        "signature": "2019-06-18-5d08445ebf72d.png",
        "date_of_signature": "2019-12-14",
        "place_of_signature": "1",
        "order_id": null,
        "agent_id": null,
        "created_at": "2019-06-18 07:54:38",
        "updated_at": "2019-06-18 07:54:38"
    },
    {
        "id": 24,
        "fkuser_id": 17,
        "assessee_year_from": "2001",
        "assessee_year_to": "2002",
        "section_822BB": "y",
        "assessee_name": "Ashiq",
        "gender": "m",
        "tin": "123456789876",
        "old_tin": "6456456",
        "circle": "4565645",
        "zone": "dfgdfgd",
        "resident_status": "r",
        "govt_employee_status": "y",
        "person_category": "10a",
        "date_of_birth": "2019-12-14",
        "income_year_from": "2001",
        "income_year_to": "2002",
        "employer_name": "fsfsdf",
        "spouse_name": "fgdfg",
        "spouse_tin": "5464545",
        "fathers_name": "uytyutyu",
        "mothers_name": "tutyutyu",
        "present_address": "tyutyu",
        "permanent_address": "tyutyutyu",
        "city_corporation": "yutyutyuty",
        "contact": "7567567",
        "email": "utyu@tr.com",
        "nid": "564564565",
        "bid": "564565",
        "signature": "2019-06-18-5d0832c6c309f.png",
        "date_of_signature": "2019-12-14",
        "place_of_signature": "1",
        "order_id": null,
        "agent_id": null,
        "created_at": "2019-06-18 06:39:34",
        "updated_at": "2019-06-18 06:39:34"
    },
    {
        "id": 22,
        "fkuser_id": 17,
        "assessee_year_from": "2001",
        "assessee_year_to": "2002",
        "section_822BB": "y",
        "assessee_name": "Ashiq",
        "gender": "m",
        "tin": "123456789876",
        "old_tin": "6456456",
        "circle": "4565645",
        "zone": "dfgdfgd",
        "resident_status": "r",
        "govt_employee_status": "y",
        "person_category": "10a",
        "date_of_birth": "2019-12-14",
        "income_year_from": "2001",
        "income_year_to": "2002",
        "employer_name": "fsfsdf",
        "spouse_name": "fgdfg",
        "spouse_tin": "5464545",
        "fathers_name": "uytyutyu",
        "mothers_name": "tutyutyu",
        "present_address": "tyutyu",
        "permanent_address": "tyutyutyu",
        "city_corporation": "yutyutyuty",
        "contact": "7567567",
        "email": "utyu@tr.com",
        "nid": "564564565",
        "bid": "564565",
        "signature": "2019-06-17-5d07265b49ba8.png",
        "date_of_signature": "2019-12-14",
        "place_of_signature": null,
        "order_id": 58,
        "agent_id": null,
        "created_at": "2019-06-17 11:34:19",
        "updated_at": "2019-06-17 13:15:54"
    },
    {
        "id": 15,
        "fkuser_id": 17,
        "assessee_year_from": "2018",
        "assessee_year_to": "2019",
        "section_822BB": "y",
        "assessee_name": "abdur",
        "gender": "m",
        "tin": "123456789098",
        "old_tin": "456456",
        "circle": "yrtyrty",
        "zone": "4565",
        "resident_status": "r",
        "govt_employee_status": "y",
        "person_category": "10a",
        "date_of_birth": "2019-04-16",
        "income_year_from": "2017",
        "income_year_to": "2018",
        "employer_name": "tyhtyu",
        "spouse_name": "utyutyu",
        "spouse_tin": "7567567",
        "fathers_name": "567567",
        "mothers_name": "utyuty",
        "present_address": "yutyu",
        "permanent_address": "tyutyu",
        "city_corporation": "Other City Corporation",
        "contact": "67567",
        "email": "fsd@gf.com",
        "nid": "123456543245",
        "bid": "6545645",
        "signature": "2019-05-18-5cdf548260f6c.png",
        "date_of_signature": "2019-04-16",
        "place_of_signature": "1",
        "order_id": 32,
        "agent_id": null,
        "created_at": "2019-05-18 06:40:34",
        "updated_at": "2019-06-20 08:29:48"
    },
    {
        "id": 14,
        "fkuser_id": 17,
        "assessee_year_from": "2000",
        "assessee_year_to": "2001",
        "section_822BB": "y",
        "assessee_name": "abdur Rahman",
        "gender": "m",
        "tin": "123456789098",
        "old_tin": "456456",
        "circle": "yrtyrty",
        "zone": "4565",
        "resident_status": "r",
        "govt_employee_status": "y",
        "person_category": "10c",
        "date_of_birth": "2019-04-16",
        "income_year_from": "1999",
        "income_year_to": "2001",
        "employer_name": "tyhtyu",
        "spouse_name": "utyutyu",
        "spouse_tin": "7567567",
        "fathers_name": "567567",
        "mothers_name": "utyuty",
        "present_address": "yutyu",
        "permanent_address": "tyutyu",
        "city_corporation": "Other City Corporation",
        "contact": "67567",
        "email": "fsd@gf.com",
        "nid": "123456543245",
        "bid": "6545645",
        "signature": "2019-05-18-5cdf542d653e7.png",
        "date_of_signature": "2019-04-16",
        "place_of_signature": "1",
        "order_id": 31,
        "agent_id": null,
        "created_at": "2019-05-18 06:39:09",
        "updated_at": "2019-05-18 11:44:51"
    },
    {
        "id": 12,
        "fkuser_id": 17,
        "assessee_year_from": "2001",
        "assessee_year_to": "2002",
        "section_822BB": "y",
        "assessee_name": "Ashiq",
        "gender": "m",
        "tin": "123456789876",
        "old_tin": "6456456",
        "circle": "4565645",
        "zone": "dfgdfgd",
        "resident_status": "r",
        "govt_employee_status": "y",
        "person_category": "10a",
        "date_of_birth": "2019-12-14",
        "income_year_from": "2001",
        "income_year_to": "2002",
        "employer_name": "fsfsdf",
        "spouse_name": "fgdfg",
        "spouse_tin": "5464545",
        "fathers_name": "uytyutyu",
        "mothers_name": "tutyutyu",
        "present_address": "tyutyu",
        "permanent_address": "tyutyutyu",
        "city_corporation": "yutyutyuty",
        "contact": "7567567",
        "email": "utyu@tr.com",
        "nid": "564564565",
        "bid": "564565",
        "signature": "2019-05-18-5cdf53f390177.png",
        "date_of_signature": "2019-12-14",
        "place_of_signature": "1",
        "order_id": null,
        "agent_id": null,
        "created_at": "2019-05-18 06:38:11",
        "updated_at": "2019-05-18 06:38:11"
    },
    {
        "id": 6,
        "fkuser_id": 17,
        "assessee_year_from": "2010",
        "assessee_year_to": "2011",
        "section_822BB": "y",
        "assessee_name": "hfghfg",
        "gender": "m",
        "tin": "123456789098",
        "old_tin": "656456",
        "circle": "45645",
        "zone": "45645",
        "resident_status": "nr",
        "govt_employee_status": "y",
        "person_category": "10a",
        "date_of_birth": "2019-05-08",
        "income_year_from": "2009",
        "income_year_to": "2010",
        "employer_name": "hghfgh",
        "spouse_name": "645645",
        "spouse_tin": "4564565",
        "fathers_name": "45645",
        "mothers_name": "456456",
        "present_address": "64565",
        "permanent_address": "456456",
        "city_corporation": "Dhaka/Chittagong City Corporation",
        "contact": "4564565",
        "email": "sdw@ds.com",
        "nid": "456456456",
        "bid": "456456",
        "signature": "2019-05-16 060251.jpg",
        "date_of_signature": "2019-05-24",
        "place_of_signature": "3",
        "order_id": null,
        "agent_id": null,
        "created_at": "2019-05-16 06:02:51",
        "updated_at": "2019-05-16 06:02:51"
    },
    {
        "id": 5,
        "fkuser_id": 17,
        "assessee_year_from": "2014",
        "assessee_year_to": "2015",
        "section_822BB": "y",
        "assessee_name": "dfsdf",
        "gender": "m",
        "tin": "1234567890987",
        "old_tin": "sdfsdf",
        "circle": "sdfsdf",
        "zone": "sdfsdf",
        "resident_status": "nr",
        "govt_employee_status": "y",
        "person_category": "10a",
        "date_of_birth": "2019-05-24",
        "income_year_from": "2013",
        "income_year_to": "2014",
        "employer_name": "sdfsder",
        "spouse_name": "sdfsdfs",
        "spouse_tin": "ergdfgdf",
        "fathers_name": "fsdf",
        "mothers_name": "sdf",
        "present_address": "sdfsdf",
        "permanent_address": "sdfsdf",
        "city_corporation": "Dhaka/Chittagong City Corporation",
        "contact": "45345",
        "email": "def@ds.com",
        "nid": "45345",
        "bid": "3454",
        "signature": "2019-05-12 074918.jpg",
        "date_of_signature": "2019-05-16",
        "place_of_signature": "2",
        "order_id": 27,
        "agent_id": null,
        "created_at": "2019-05-12 07:49:18",
        "updated_at": "2019-05-12 07:49:40"
    },
    {
        "id": 4,
        "fkuser_id": 17,
        "assessee_year_from": "2010",
        "assessee_year_to": "2011",
        "section_822BB": "y",
        "assessee_name": "asdas",
        "gender": "m",
        "tin": "asdassds",
        "old_tin": "dasdsd",
        "circle": "sadasd",
        "zone": "sadsd",
        "resident_status": "r",
        "govt_employee_status": "y",
        "person_category": "10a",
        "date_of_birth": "2019-05-15",
        "income_year_from": "2009",
        "income_year_to": "2010",
        "employer_name": "dffsdf",
        "spouse_name": "sdfsdf",
        "spouse_tin": "sdfdfs",
        "fathers_name": "sdfdfsdf",
        "mothers_name": "sdfsdf",
        "present_address": "sdfsdf",
        "permanent_address": "sdfsdf",
        "city_corporation": "Dhaka/Chittagong City Corporation",
        "contact": "5411",
        "email": "fsd@df.com",
        "nid": "5165416",
        "bid": "564654",
        "signature": "2019-05-11 053337.jpg",
        "date_of_signature": "2019-05-16",
        "place_of_signature": "1",
        "order_id": 26,
        "agent_id": null,
        "created_at": "2019-05-11 05:33:37",
        "updated_at": "2019-05-11 05:33:51"
    }
  ]
}

Я хочу проанализировать массив «response» с декодером и добавить в массив, не отслеживая каждое свойство массива response, используя /value(forKey: "propertyName")

Я попробовал это, создав пользовательский basicInfoList struct

if let basiclInfoList =  try? JSONDecoder().decode([BasicInfoList].self, from: data){                   print(basilInfoList)
}

Ответы [ 2 ]

0 голосов
/ 26 июня 2019

Поскольку ваш JSON-ответ является Dictionary, а не Array, поэтому вы не можете напрямую использовать [BasicInfoList].self для анализа ответа.

Вам необходимо создать моделькак показано ниже,

struct Root: Codable {
    let response: [BasicInfoList]
}

struct BasicInfoList: Codable {
    let id: Int
    //add only properties you want to parse
}

Разбор вашего JSON с использованием,

do {
    let decoder = JSONDecoder()
    decoder.keyDecodingStrategy = .convertFromSnakeCase
    let root = try decoder.decode(Root.self, from: data)
    print(root.response)
} catch {
    print(error)
}

Примечание: С Codable вы не сможетедекодировать все без указания каждого key и его value type.Any не работает в Codable.

0 голосов
/ 26 июня 2019

Необходимо реализовать протокол Decodable в структуре.

См .: https://www.swiftbysundell.com/basics/codable (или любую другую ссылку, ищущую "using Codable in swift")

Небольшой советВ Swift 4.2 вы можете использовать специальное декодирование из случая змеи, чтобы не реализовывать высокое имя в ключах кодирования, когда вы реализуете Codable в своей структуре.Смотри: https://developer.apple.com/documentation/foundation/jsondecoder/keydecodingstrategy/convertfromsnakecase

...