Я новичок здесь, и я разбираю json, который является массивом в боковом массиве, поэтому я не могу понять, как получить массив из json, позвольте мне показать вам мой json.
JSON
{
"subject_list" = (
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
"sub_list" = (
{
"ch_id" = 17;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1530600693.jpg";
"ch_name" = " 01. Measurement";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
},
{
"ch_id" = 23;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1451930609.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
},
{
"ch_id" = 24;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1884777188.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
},
{
"ch_id" = 25;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1518702048.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 4;
}
);
"sub_name" = Physics;
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 8;
"sub_list" = (
{
"ch_id" = 26;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1437196139.jpg";
"ch_name" = " 1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 8;
},
{
"ch_id" = 27;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1903171865.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 8;
}
);
"sub_name" = Chemistry;
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 9;
"sub_list" = (
{
"ch_id" = 31;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1319333294.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 9;
}
);
"sub_name" = Testing;
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 10;
"sub_list" = (
{
"ch_id" = 28;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1373218664.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 10;
}
);
"sub_name" = "Test Subject";
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 11;
"sub_list" = (
{
"ch_id" = 29;
"ch_image" = "http://mobileapp.xmeducation.com/upload/246189282.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 11;
}
);
"sub_name" = "Test Subject 1";
},
{
"con_id" = 2;
"level_id" = 1;
"sub_id" = 12;
"sub_list" = (
{
"ch_id" = 30;
"ch_image" = "http://mobileapp.xmeducation.com/upload/1342731807.jpg";
"ch_name" = "1. Test Chapter";
"con_id" = 2;
"level_id" = 1;
"sub_id" = 12;
}
);
"sub_name" = "Test Subject 2";
}
);
это мой JSON, и я хочу sub_list
массив из subject_list
Я сделал некоторый код, позвольте мне показать вам.
КОД
func callSubChapAPI(){
let preferences = UserDefaults.standard
let studentlvl = "student_lvl"
let student_lvl = preferences.object(forKey: studentlvl) as! String
print(student_lvl)
let params = ["level_id": student_lvl]
Alamofire.request(subListWithChapter, method: .post, parameters: params).responseJSON(completionHandler: {(response) in
switch response.result{
case.success(let data):
print(data)
let json = JSON(data)
print(json)
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let subjects = try decoder.decode(SubjectResponse.self, from: data as! Data)
print(subjects)
} catch {
print(error)
}
case.failure(let error):
print(error.localizedDescription)
}
})
}
вот моя структура:
struct sub_list {
let ch_id : Int
let ch_image: String
let ch_name: String
let con_id: String
let level_id: String
let sub_id: String
}
Пожалуйста, посмотрите мой код и формат json, пожалуйста, скажите мне, как получить массив sub_list
от JSON, пожалуйста, черт возьми, и спасибо заранее.