如何解决在 Swift 中的 Array of Array(2D Array) 中追加 Json 数据
{
"status": 200,"message": "Success!","result": {
"LessonDetail": [
{
"id": 78,"lessonjs": "<script src=\"https://learnspeakthai.com/js/lesson.js\" ></script>","name": "Question words #3: What?","code": "ST1C2L026","chapter_id": "12","sort_order": "25","content_top": "","image": "https://learnspeakthai.com/uploads/images/lesson/lesson_thumb/8b2cbbca4a711009806b915971478cb54b2ab0d1.jpg","show_tone_practice": "0","tone_practice_shown": "None","tone_practise_data": "","show_slideshow": "1","active": "1","created_at": "2011-07-28T02:51:34.000000Z","updated_at": "2014-10-24T20:24:17.000000Z","pdf": "","is_practise_data": 0
}
],"SubLesson": [
{
"id": 86,"name": "Question words 3: What?","show_name": "0","code": "ST1C2L027","lesson_id": "78","sort_order": "5","content_top": "‘³A-ry?’ = ‘what?’ It is always used at the end of sentence.","slide": [
{
"id": 321,"sub_lesson_id": "86","code": "ST1C2L028","isaana": "³A-ry","thai": "อะไร","english": "What? ","english_word_order": "","language_id": "1","slide": "https://learnspeakthai.com/uploads/images/slide/34322cde0baa04df7ed58295863e390c7a489a98.jpg","pre_slide": "https://learnspeakthai.com/uploads/images/slide/ff791dbc630a4407eec2d4bdb4cbf677fac820bb.jpg","audio_en": "https://learnspeakthai.com/uploads/audio/audio_en/ed99dca146d83b40c8a91b1a2cc58844fa9d8162.mp3","audio_th": "https://learnspeakthai.com/uploads/audio/audio_th/5ca41fb350d2713194b00071fab749d36d376f8c.mp3","sort_order": null,"pause": "0","created_at": "2011-07-28T02:55:06.000000Z","updated_at": "2011-07-28T02:55:21.000000Z","language_name": "Thai","language_code": "TH"
},{
"id": 322,"code": "ST1C2L029","isaana": "(Kun) cªheu ³a-ry?","thai": "คุณชื่ออะไร","english": "What’s your name?","english_word_order": "(You name what?)","slide": "https://learnspeakthai.com/uploads/images/slide/fff77db9e63b1ed203ef186c1252dee9ee47f1e6.jpg","pre_slide": "https://learnspeakthai.com/uploads/images/slide/85315f3e7892fdefff80dbc17a140fed7a1a0812.jpg","audio_en": "https://learnspeakthai.com/uploads/audio/audio_en/158079fe8d6aab8d95201d2c945a378b971a5e04.mp3","audio_th": "https://learnspeakthai.com/uploads/audio/audio_th/d9f7c4170a13336b95846b995add06f3537e6730.mp3","created_at": "2011-07-28T02:56:23.000000Z","updated_at": "2011-08-19T00:20:38.000000Z",{
"id": 323,"code": "ST1C2L030","isaana": "Cªheu B°orb","thai": "ชื่อบ๊อบ","english": "My name is Bob ","english_word_order": "(Name Bob)","slide": "https://learnspeakthai.com/uploads/images/slide/783d893e197d718c9d32624cf6aa6eff25113dc2.jpg","pre_slide": "https://learnspeakthai.com/uploads/images/slide/fae572b408441871d16d9335675f88895c533f4e.jpg","audio_en": "https://learnspeakthai.com/uploads/audio/audio_en/7b80d533c30f9d7839a1cd5f058c10092960dcf8.mp3","audio_th": "https://learnspeakthai.com/uploads/audio/audio_th/57ce04c653580cdf525af9b49d8deb19f49c9786.mp3","created_at": "2011-07-28T02:58:00.000000Z","updated_at": "2011-07-28T02:58:17.000000Z",{
"id": 324,"code": "ST1C2L031","isaana": "An-n°ee r§iak w§ah ³a-ry?","thai": "อันนี้เรียกว่าอะไร","english": "What’s this called?","english_word_order": "(This called what?)","slide": "https://learnspeakthai.com/uploads/images/slide/8244e536e5bcd1f0383b322f0df04a31926888f2.jpg","pre_slide": "https://learnspeakthai.com/uploads/images/slide/0c31968d79a921bf8b2810d8b05b9d01c4f6898f.jpg","audio_en": "https://learnspeakthai.com/uploads/audio/audio_en/394021ec2ff89c15a607ce3fd31ba0835115b84a.mp3","audio_th": "https://learnspeakthai.com/uploads/audio/audio_th/9034801d15fb058ea4a2bc0ba545e92cc3ec63a2.mp3","created_at": "2011-07-28T02:59:24.000000Z","updated_at": "2011-07-28T03:00:02.000000Z",{
"id": 325,"code": "ST1C2L032","isaana": " R§iak w§ah d§ton °my ","thai": " เรียกว่าต้นไม้ ","english": "It’s called a tree","slide": "https://learnspeakthai.com/uploads/images/slide/2725e831aaf141324cc62dc61300b95dd544d487.jpg","pre_slide": "https://learnspeakthai.com/uploads/images/slide/b95be383542085e2c3ef1500c86c7c432c8155f9.jpg","audio_en": "https://learnspeakthai.com/uploads/audio/audio_en/e66433b0437f6b1b6d0f65b61c8b13b4e27fd57d.mp3","audio_th": "https://learnspeakthai.com/uploads/audio/audio_th/88b3abbec52de4dc667cba0b2fc4db1e1ffc42cb.mp3","created_at": "2011-07-28T03:01:02.000000Z","updated_at": "2018-05-15T09:20:24.000000Z","language_code": "TH"
}
],"content_bottom": "","image": "","created_at": "2011-07-28T02:54:09.000000Z","updated_at": "2011-08-08T03:51:52.000000Z"
},{
"id": 87,"name": "add the polite particle ‘³na’","code": "ST1C2L033","sort_order": "10","content_top": "For politeness,when you use the word ‘what?’ on its own you should always add the polite particle ‘³na’. Note: This is a different polite particle than the ‘°na’ used in conjunction with ‘k°rap’ / ‘§ka’. ",{
"id": 326,"sub_lesson_id": "87","code": "ST1C2L033a","isaana": "³A-ry ³na?","thai": "อะไรน่ะ","english": "What? / pardon?","english_word_order": "What p.p.? ","slide": "https://learnspeakthai.com/uploads/images/slide/c2e01e9bdc3455275a60fc58640fef574920931a.jpg","pre_slide": "https://learnspeakthai.com/uploads/images/slide/1c9490fdc1ae25dacdecba8d5054b8bc2e116595.jpg","audio_en": "https://learnspeakthai.com/uploads/audio/audio_en/c96b08c9ee6afdd3db503abb83dbec673768284b.mp3","audio_th": "https://learnspeakthai.com/uploads/audio/audio_th/dbfede0423eb13e4fae61f0bfe3f45ef52602868.mp3","created_at": "2011-07-28T03:04:40.000000Z","updated_at": "2021-02-16T21:57:32.000000Z","created_at": "2011-07-28T03:03:11.000000Z","updated_at": "2021-02-16T21:55:34.000000Z"
}
],"GameList": [
{
"id": 1189,"name": "Question words #1: Matching","code": "ST1C2G001a","game_type": "matching","word_source": "selection_from_all","words_to_display": "10","is_free": 0,"is_subscription": 1,"created_at": "2014-10-07T06:38:48.000000Z","updated_at": "2014-10-07T10:25:07.000000Z"
},{
"id": 1190,"name": "Question words #2: Flash card","code": "ST1C2G001b","game_type": "flashcard","sort_order": "6","created_at": "2014-10-07T06:57:22.000000Z","updated_at": "2014-10-07T10:25:38.000000Z"
}
],"Slideshow": [
{
"id": 321,"audio_th": "https://learnspeakthai.com/uploads/audio/audio_th/5ca41fb350d2713194b00071fab749d36d376f8c.mp3"
},{
"id": 322,"audio_th": "https://learnspeakthai.com/uploads/audio/audio_th/d9f7c4170a13336b95846b995add06f3537e6730.mp3"
},{
"id": 323,"audio_th": "https://learnspeakthai.com/uploads/audio/audio_th/57ce04c653580cdf525af9b49d8deb19f49c9786.mp3"
},{
"id": 324,"audio_th": "https://learnspeakthai.com/uploads/audio/audio_th/9034801d15fb058ea4a2bc0ba545e92cc3ec63a2.mp3"
},{
"id": 325,"audio_th": "https://learnspeakthai.com/uploads/audio/audio_th/88b3abbec52de4dc667cba0b2fc4db1e1ffc42cb.mp3"
},{
"id": 326,"audio_th": "https://learnspeakthai.com/uploads/audio/audio_th/dbfede0423eb13e4fae61f0bfe3f45ef52602868.mp3"
}
]
}
}
我已经为 json 创建了模型。我想在来自 json 的英文键数组中附加值。
我在做——
var english_Array = [[String]] ()
guard let subLesson = userResponse.result?.subLesson else {return}
for i in subLesson {
english_Array.append(i.slide?[0].english ?? "" )
}
print(english_Array)
值:
[["What?"],["What?]]
我需要-
[["What?","What’s your name?","My name is Bob","What’s this called?","It’s called a tree"],["What?","It’s called a tree","What? / pardon?"]]
解决方法
首先,同一个键“slide”有两个值。你应该修复它。现在让我们考虑它们在一个数组 [slide1,slide2] 中,用于键“slides”。
var english_Array = [[String]] ()
for slide in subLesson.slides {
let english = slide.map { $0.english ?? "" }
english_Array.append(english_Array)
}
应该这样做。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。