解析嵌套的 json 问题
Posted
技术标签:
【中文标题】解析嵌套的 json 问题【英文标题】:parsing nested json issue 【发布时间】:2018-12-18 05:29:35 【问题描述】:我是新来的,我正在解析 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,我想要来自 subject_list
的 sub_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 格式请告诉我如何从 json 中获取 sub_list
数组,请见鬼,提前致谢。
【问题讨论】:
【参考方案1】:实际上subject_list
也是一个array
,所以你也需要对其进行迭代。
let data = json["subject_list"]
print(data)
data.array?.forEach( (subject) in
subject["sub_list"].array?.forEach( (chapList) in
let chapter = sub_list(ch_id: chapList["ch_id"].intValue, ch_image: chapList["ch_image"].stringValue, ch_name: chapList["ch_name"].stringValue, con_id: chapList["con_id"].stringValue, level_id: chapList["level_id"].stringValue, sub_id: chapList["sub_id"].stringValue)
print(chapter)
self.chapListData.append(chapter)
)
)
self.collView.reloadData()
推荐
当您已经有 Apple 提供的 Codable
时,不值得为 json
解析添加任何依赖项。由于SwiftyJSON
,它将减少您添加的大部分样板代码。
以下是您当前 api 的完整示例,
struct SubjectResponse: Decodable
let subjectList: [Subject]
struct Subject: Decodable
let subList: [Chapter]
struct Chapter: Decodable
let chId : Int
let chImage: String
let chName: String
let conId: Int
let levelId: Int
let subId: Int
Alamofire.request(subListWithChapter, method: .post, parameters: params).responseData() (response) in
switch response.result
case .success(let data):
do
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let subjects = try decoder.decode(SubjectResponse.self, from: data)
print(subjects)
catch
print(error)
case .failure(let error):
print(error.localizedDescription)
【讨论】:
当我将您的代码放入数据时,它会给出这样的建议 无法将“任何”类型的值转换为预期的参数类型“数据”,并且当我插入时!它给了我崩溃数据 错误类似于:无法将类型“__NSSingleEntryDictionaryI”(0x1c6c8dcc0)的值转换为“NSData”(0x1c6c8ddd8)。 2018-12-18 12:22:13.912859+0530 XMEducation[5496:1296891] 无法将“__NSSingleEntryDictionaryI”(0x1c6c8dcc0)类型的值转换为“NSData”(0x1c6c8ddd8)。 你必须使用.responseJSON
,而你必须使用responseData()
。复制完整的示例。
我复制了但仍然给我问题
你在这里使用了错误的方法Alamofire.request(subListWithChapter, method: .post, parameters: params).responseJSON
。只需复制我上面提供的整个示例即可。以上是关于解析嵌套的 json 问题的主要内容,如果未能解决你的问题,请参考以下文章