如何手动解码 swift 中的 json 数据 4
How to manually decode json data in swift 4
如何手动按键解码[相册]和[图片]?
我试过了,但我做不到。
不知错误是什么?
谢谢 !
我的 Json
http://appscorporation.ga/api-user/test
struct ProfileElement: Codable {
let user: User
let postImage: String
let postLikes: Int
let postTags: String
enum CodingKeys: String, CodingKey {
case user
case postImage = "post_image"
case postLikes = "post_likes"
case postTags = "post_tags"
}
}
struct User: Codable {
let name, surname: String
let profilePic: String
let albums: [Album]
enum CodingKeys: String, CodingKey {
case name, surname
case profilePic = "profile_pic"
case albums
}
}
第二块
struct Album {
let id: Int
let title: String
var images: [Image]
enum AlbumKeys: String, CodingKey {
case id = "id"
case title = "title"
case images = "images"
}
}
struct Image: Codable {
let id: Int
let url: String
enum CCodingKeys: String, CodingKey {
case id = "id"
case url = "url"
}
}
你收到错误
Type 'User' does not conform to protocol 'Decodable'
因为所有的struct都要采用(De)codable
,
如果您稍作改动,这些结构就会起作用
struct ProfileElement: Decodable {
let user: User
let postImage: String
let postLikes: Int
let postTags: String
}
struct User: Decodable {
let name, surname: String
let profilePic: String
let albums: [Album]
}
struct Album : Decodable {
let id: Int
let title: String
var images: [Image]
}
struct Image: Decodable {
let id: Int
let url: String
}
然后解码
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase // This line gets rid of all CodingKeys
let result = try decoder.decode([ProfileElement].self, from: data)
你可以用
得到每张图片
for item in result {
for album in item.user.albums {
for image in album.images {
print(image)
}
}
}
如何手动按键解码[相册]和[图片]? 我试过了,但我做不到。 不知错误是什么? 谢谢 ! 我的 Json http://appscorporation.ga/api-user/test
struct ProfileElement: Codable {
let user: User
let postImage: String
let postLikes: Int
let postTags: String
enum CodingKeys: String, CodingKey {
case user
case postImage = "post_image"
case postLikes = "post_likes"
case postTags = "post_tags"
}
}
struct User: Codable {
let name, surname: String
let profilePic: String
let albums: [Album]
enum CodingKeys: String, CodingKey {
case name, surname
case profilePic = "profile_pic"
case albums
}
}
第二块
struct Album {
let id: Int
let title: String
var images: [Image]
enum AlbumKeys: String, CodingKey {
case id = "id"
case title = "title"
case images = "images"
}
}
struct Image: Codable {
let id: Int
let url: String
enum CCodingKeys: String, CodingKey {
case id = "id"
case url = "url"
}
}
你收到错误
Type 'User' does not conform to protocol 'Decodable'
因为所有的struct都要采用(De)codable
,
如果您稍作改动,这些结构就会起作用
struct ProfileElement: Decodable {
let user: User
let postImage: String
let postLikes: Int
let postTags: String
}
struct User: Decodable {
let name, surname: String
let profilePic: String
let albums: [Album]
}
struct Album : Decodable {
let id: Int
let title: String
var images: [Image]
}
struct Image: Decodable {
let id: Int
let url: String
}
然后解码
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase // This line gets rid of all CodingKeys
let result = try decoder.decode([ProfileElement].self, from: data)
你可以用
得到每张图片for item in result {
for album in item.user.albums {
for image in album.images {
print(image)
}
}
}