1

Я делаю запрос через Alamofire:

request(req, method: .get).responseJSON(completionHandler: {response in
            switch response.result {
            case .success(let value):
                print(value)
            case .failure(let error):
                print("error", error)
            }
        })

Получаю следующий ответ:

{
    response =     {
        count = 4;
        items =         (
                        {
                "album_id" = "-6";
                date = 1537797877;
                id = 456243462;
                likes =                 {
                    count = 17;
                    "user_likes" = 0;
                };
                "owner_id" = 251774;
                "post_id" = 236;
                reposts =                 {
                    count = 0;
                };
                sizes =                 (
                                        {
                        height = 73;
                        type = m;
                        url = "https://pp.userapi.com/c848632/v848632127/7b0cd/c8I_L01BM.jpg";
                        width = 130;
                    },
                                        {
                        height = 87;
                        type = o;
                        url = "https://pp.userapi.com/c848632/v848632127/7b0d1/DLur6xsw.jpg";
                        width = 130;
                    },
                                        {
                        height = 133;
                        type = p;
                        url = "https://pp.userapi.com/c848632/v848632127/7b0d2/EAcJyKi4.jpg";
                        width = 200;
                    },
                                        {
                        height = 213;
                        type = q;
                        url = "https://pp.userapi.com/c848632/v848632127/7b0d3/DFjeG6Z0.jpg";
                        width = 320;
                    },
                                        {
                        height = 340;
                        type = r;
                        url = "https://pp.userapi.com/c848632/v848632127/7b0d4/KwhWjd_g.jpg";
                        width = 510;
                    },
                                        {
                        height = 42;
                        type = s;
                        url = "https://pp.userapi.com/c848632/v848632127/7b0cc/jKHsWtjg.jpg";
                        width = 75;
                    },
                                        {
                        height = 340;
                        type = x;
                        url = "https://pp.userapi.com/c848632/v848632127/7b0ce/3lqSpDUw.jpg";
                        width = 604;
                    },
                                        {
                        height = 454;
                        type = y;
                        url = "https://pp.userapi.com/c848632/v848632127/7b0cf/NP4tOrbM.jpg";
                        width = 807;
                    },
                                        {
                        height = 720;
                        type = z;
                        url = "https://pp.userapi.com/c848632/v848632127/7b0d0/rsYo-5sA.jpg";
                        width = 1280;
                    }
                );
                text = "";
            },
                        {
                "album_id" = "-6";
                date = 1536088988;
                id = 456243271;
                likes =                 {
                    count = 9;
                    "user_likes" = 0;
                };
                "owner_id" = 251774;
                "post_id" = 230;
                reposts =                 {
                    count = 0;
                };
                sizes =                 (
                                        {
                        height = 124;
                        type = m;
                        url = "https://pp.userapi.com/c830408/v830408750/18ee1f/4__sl8rej6M.jpg";
                        width = 130;
                    },
                                        {
                        height = 124;
                        type = o;
                        url = "https://pp.userapi.com/c830408/v830408750/18ee21/_QPPOPO7ohQ.jpg";
                        width = 130;
                    },
                                        {
                        height = 191;
                        type = p;
                        url = "https://pp.userapi.com/c830408/v830408750/18ee22/EY-ffSixRzE.jpg";
                        width = 200;
                    },
                                        {
                        height = 305;
                        type = q;
                        url = "https://pp.userapi.com/c830408/v830408750/18ee23/93wyYVboGOM.jpg";
                        width = 320;
                    },
                                        {
                        height = 343;
                        type = r;
                        url = "https://pp.userapi.com/c830408/v830408750/18ee24/Z1nymZ2GW2g.jpg";
                        width = 360;
                    },
                                        {
                        height = 72;
                        type = s;
                        url = "https://pp.userapi.com/c830408/v830408750/18ee1e/SjXIGN6PjLo.jpg";
                        width = 75;
                    },
                                        {
                        height = 343;
                        type = x;
                        url = "https://pp.userapi.com/c830408/v830408750/18ee20/eX4nWeoY0_M.jpg";
                        width = 360;
                    }
                );
                text = "";
            },
                        {
                "album_id" = "-6";
                date = 1528439418;
                id = 456242925;
                likes =                 {
                    count = 26;
                    "user_likes" = 0;
                };
                "owner_id" = 251774;
                "post_id" = 229;
                reposts =                 {
                    count = 0;
                };
                sizes =                 (
                                        {
                        height = 130;
                        type = m;
                        url = "https://pp.userapi.com/c847219/v847219818/74196/gA48sryD8gs.jpg";
                        width = 96;
                    },
                                        {
                        height = 175;
                        type = o;
                        url = "https://pp.userapi.com/c847219/v847219818/7419a/R8r0CNcUVVA.jpg";
                        width = 130;
                    },
                                        {
                        height = 270;
                        type = p;
                        url = "https://pp.userapi.com/c847219/v847219818/7419b/soa1T32BIAM.jpg";
                        width = 200;
                    },
                                        {
                        height = 432;
                        type = q;
                        url = "https://pp.userapi.com/c847219/v847219818/7419c/pNkjJyCiqGU.jpg";
                        width = 320;
                    },
                                        {
                        height = 688;
                        type = r;
                        url = "https://pp.userapi.com/c847219/v847219818/7419d/bBS8IJrAXmw.jpg";
                        width = 510;
                    },
                                        {
                        height = 75;
                        type = s;
                        url = "https://pp.userapi.com/c847219/v847219818/74195/TiTeNU7C7LM.jpg";
                        width = 55;
                    },
                                        {
                        height = 604;
                        type = x;
                        url = "https://pp.userapi.com/c847219/v847219818/74197/2Z7vs-lEeL0.jpg";
                        width = 448;
                    },
                                        {
                        height = 807;
                        type = y;
                        url = "https://pp.userapi.com/c847219/v847219818/74198/ebOd5gvyuyQ.jpg";
                        width = 598;
                    },
                                        {
                        height = 1052;
                        type = z;
                        url = "https://pp.userapi.com/c847219/v847219818/74199/ueKjlj-F4S8.jpg";
                        width = 780;
                    }
                );
                text = "";
            },
                        {
                "album_id" = "-6";
                date = 1461593979;
                id = 413019118;
                likes =                 {
                    count = 30;
                    "user_likes" = 0;
                };
                "owner_id" = 251774;
                "post_id" = 201;
                reposts =                 {
                    count = 0;
                };
                sizes =                 (
                                        {
                        height = 130;
                        type = m;
                        url = "https://pp.userapi.com/c622618/v622618609/2fc9d/nyBc3id65Wk.jpg";
                        width = 130;
                    },
                                        {
                        height = 130;
                        type = o;
                        url = "https://pp.userapi.com/c622618/v622618609/2fc9f/E_4HDl7qyDQ.jpg";
                        width = 130;
                    },
                                        {
                        height = 200;
                        type = p;
                        url = "https://pp.userapi.com/c622618/v622618609/2fca0/B9SUzsJW7Ew.jpg";
                        width = 200;
                    },
                                        {
                        height = 320;
                        type = q;
                        url = "https://pp.userapi.com/c622618/v622618609/2fca1/tih24_scXhw.jpg";
                        width = 320;
                    },
                                        {
                        height = 510;
                        type = r;
                        url = "https://pp.userapi.com/c622618/v622618609/2fca2/Ww6nIOiWZe0.jpg";
                        width = 510;
                    },
                                        {
                        height = 75;
                        type = s;
                        url = "https://pp.userapi.com/c622618/v622618609/2fc9c/eITzqE9lGu4.jpg";
                        width = 75;
                    },
                                        {
                        height = 600;
                        type = x;
                        url = "https://pp.userapi.com/c622618/v622618609/2fc9e/ma4u3oPlVeo.jpg";
                        width = 600;
                    }
                );
                text = "";
            }
        );
    };
}

Как полученный JSON перевести в словарь, чтобы можно было обращаться по элементам?

Все мои попытки заканчивались ошибками.

1

1 ответ 1

0

Попробуйте так

Alamofire.request(req, method: .get).responseJSON(completionHandler: {response in
            switch response.result {
            case .success(let value):
                 if let dictionary = value as? [String: AnyObject] {
                     print(dictionary )
                 }
            case .failure(let error):
                print("error", error)
            }
        })

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.