如何使用Codable解析Swift 4中的复杂JSON
我需要从服务器解析JSON的帮助。以下是JSON:如何使用Codable解析Swift 4中的复杂JSON,json,swift,codable,Json,Swift,Codable,我需要从服务器解析JSON的帮助。以下是JSON: { "response": { "items": [ { "type": "post", "source_id": -17507435, "date": 1514538602, "post_id": 4105, "post_type": "post", "text": "Some
{
"response": {
"items": [
{
"type": "post",
"source_id": -17507435,
"date": 1514538602,
"post_id": 4105,
"post_type": "post",
"text": "Some text here",
"marked_as_ads": 0,
"attachments": [
{
"type": "photo",
"photo": {
"id": 456239655,
"album_id": -7,
"owner_id": -17507435,
"user_id": 100,
"photo_75": "https://sun1-3.userapi.com/c840632/v840632924/3b7e7/4YUS7DlaLK8.jpg",
"photo_130": "https://sun1-3.userapi.com/c840632/v840632924/3b7e8/Ffpb4ZUlulI.jpg",
"photo_604": "https://sun1-3.userapi.com/c840632/v840632924/3b7e9/-pkl6Qdb9hk.jpg",
"width": 439,
"height": 312,
"text": "",
"date": 1514538602,
"post_id": 4105,
"access_key": "6a61a49570efd9c39c"
}
}
],
"post_source": {
"type": "api"
},
"comments": {
"count": 0,
"groups_can_post": true,
"can_post": 1
},
"likes": {
"count": 0,
"user_likes": 0,
"can_like": 1,
"can_publish": 1
},
"reposts": {
"count": 0,
"user_reposted": 0
},
"views": {
"count": 2
}
}
],
"profiles": [],
"groups": [
{
"id": 17507435,
"name": "Literature Museum",
"screen_name": "samlitmus",
"is_closed": 0,
"type": "group",
"is_admin": 0,
"is_member": 1,
"photo_50": "https://pp.userapi.com/c615722/v615722068/e58c/d5Y8E_5689s.jpg",
"photo_100": "https://pp.userapi.com/c615722/v615722068/e58b/Hm05ga3x2J8.jpg",
"photo_200": "https://pp.userapi.com/c615722/v615722068/e589/yoG_DDalFII.jpg"
},
{
"id": 27711883,
"name": "E:\\music\\melodic hardcore",
"screen_name": "e_melodic_hc",
"is_closed": 0,
"type": "page",
"is_admin": 0,
"is_member": 1,
"photo_50": "https://pp.userapi.com/c628220/v628220426/47092/xepNnC7pSBw.jpg",
"photo_100": "https://pp.userapi.com/c628220/v628220426/47091/uAokr-c3NQ8.jpg",
"photo_200": "https://pp.userapi.com/c628220/v628220426/4708f/eNY4vzooz4E.jpg"
},
{
"id": 81574241,
"name": "DOS4GW.EXE",
"screen_name": "dos4gw",
"is_closed": 0,
"type": "page",
"is_admin": 0,
"is_member": 1,
"photo_50": "https://pp.userapi.com/c622118/v622118651/e045/vlhV6QxtoLI.jpg",
"photo_100": "https://pp.userapi.com/c622118/v622118651/e044/P9mVUhXBV58.jpg",
"photo_200": "https://pp.userapi.com/c622118/v622118651/e043/Soq8oxCMB0I.jpg"
},
{
"id": 76709587,
"name": "Prosvet",
"screen_name": "prosvet_pub",
"is_closed": 0,
"type": "page",
"is_admin": 0,
"is_member": 0,
"photo_50": "https://pp.userapi.com/c630431/v630431500/b24a/GHox8AmDTXU.jpg",
"photo_100": "https://pp.userapi.com/c630431/v630431500/b249/H3mcC-K7htM.jpg",
"photo_200": "https://pp.userapi.com/c630431/v630431500/b248/9fyvB8gkcwc.jpg"
}
],
"next_from": "1/4105_1514494800_5"
}
}
我需要从这个JSON中获得以下行:“文本”、“评论”、“喜欢”、“转发”、“附件”
在“附件”字段中,我想获得“photo_604”行
这是我的密码:
class NewsItems: Decodable {
var text: String?
var comments: Comments
var likes: Likes
var reposts: Reposts
var attachments: [Attachments]
}
class Comments: Decodable {
var count: Int?
}
class Likes: Decodable {
var count: Int?
}
class Reposts: Decodable {
var count: Int?
}
class Attachments: Decodable {
var attachments: AttachmentPhoto
}
class AttachmentPhoto: Decodable {
var photo: WhatIsInsideAttachmentsPhoto
}
class WhatIsInsideAttachmentsPhoto: Decodable {
var photo: String?
enum CodingKeys: String, CodingKey {
case photo = "photo_604"
}
}
class WhatIsIsideResponseNewsFeed: Decodable {
var items: [NewsItems]
}
public class ResponseNewsFeed: Decodable {
var response: WhatIsIsideResponseNewsFeed
}
但在提出请求后:
Alamofire.request(baseURL+methodName, parameters: parameters).responseData(completionHandler: { response in
if let result = response.result.value {
let decoder = JSONDecoder()
let myResponse = try! decoder.decode(ResponseNewsFeed.self, from: result)
completion(myResponse.response.items)
我得到一个错误:
致命错误:“尝试!”表达式意外引发错误:
Swift.DecodingError.keyNotFound(_UL_PetrovLeonid.NewsItems)(编码键
在FA9A2FC8130449AA328C19ACD9506C2D)。附件中,
Swift.DecodingError.Context(编码路径:
[u UL_Leonid.responsenewfeed.(输入密码)
A.Ful9A2FC81309AA328 C19ACD9506C2D)响应(FA9A2F2830309AA328 C19ACD9506C2D中的键密钥)项目(基础):(Y.12768 CA107A31 EF2DCE034 FD75 B54 1C9)(StrugValue:“索引0”,InValue:Opple(0)),调试描述:“没有与密钥相关联的值”
附件(\'attachments\'”,underyingError:nil))
为什么会发生这种情况?我需要做些什么来解决它?从现在起我才开始编写代码三个月,所以如果我的问题看起来很愚蠢,请事先原谅我
谢谢。错误信息非常清楚 。。。Swift.decoding错误。未找到键新闻项目。(在FA9A2FC8130449AA328C19ACD9506C2D中编码键)。附件没有与关键附件(“附件”)关联的值
- keyNotFound是否缺少密钥
- 新闻项。(编码键…).attachments是
新闻项中键
附件的对象,它是
附件的
- 没有与关键附件(“附件”)相关的值
attachments
中没有正确的键attachments
看看你的JSON
"attachments": [
{
"type": "photo",
"photo": {
相当于
class Attachments: Decodable {
let type : String
let photo : AttachmentPhoto
}
而AttachmentPhoto
应该是
class AttachmentPhoto: Decodable {
private enum CodingKeys : String, CodingKey {
case photo604 = "photo_604"
case id
}
let id : Int
let photo604 : String // or even URL
// etc.
}
实际上,没有必要使用
类
es,在大多数情况下,一个结构就足够了。谢谢你,瓦迪安,你的答案解决了这个错误!对于像我这样的新来者来说,很容易忽视这些事情。