Я использую Tumblr v1 API (https://www.tumblr.com/docs/en/api/v1). Я использовал https://app.quicktype.io для подготовки всех структур для этого конкретного JSON. В этой документации, которую вы можете найти, возвращаемый JSON назначен переменной javaScript. IЯ не знаю, как читать данные из этой переменной / JSON. Когда я пытался прочитать некоторые параметры, появилась ошибка: данные не удалось прочитать, потому что они не в правильном формате. У вас есть идеи, как это читатьJSON?
let baseUrl = ".tumblr.com/api/read/json"
let https = "https://"
let userName = "timberland"
let jsonDecoder = JSONDecoder()
guard let mainUrl = URL(string: https + userName + baseUrl) else { return }
let urlSession = URLSession.shared
urlSession.dataTask(with: mainUrl) { (data, response, error) in
if let error = error {
print(error.localizedDescription)
}
guard let httpResponse = response as? HTTPURLResponse else { return }
if httpResponse.statusCode == 200 {
guard let data = data else { return }
do {
let myPost = try jsonDecoder.decode(TumblrJSON.self, from: data)
DispatchQueue.main.async {
print(myPost.posts[0].id)
}
} catch {
print(error.localizedDescription)
}
}
}.resume()
и структуры JSON
struct TumblrJSON: Codable {
let tumblelog: TumblrJSONTumblelog
let postsStart, postsTotal: Int
let postsType: Bool
let posts: [Post]
enum CodingKeys: String, CodingKey {
case tumblelog
case postsStart = "posts-start"
case postsTotal = "posts-total"
case postsType = "posts-type"
case posts
}
}
struct Post: Codable {
let id: String
let url, urlWithSlug: String
let type, dateGmt, date: String
let bookmarklet, mobile: Int
let feedItem: String
let fromFeedID, unixTimestamp: Int
let format, reblogKey, slug: String
let isSubmission: Bool
let likeButton, reblogButton, state, noteCount: String
let tumblelog: PostTumblelog
let regularTitle, regularBody: String
let tags: [String]
enum CodingKeys: String, CodingKey {
case id, url
case urlWithSlug = "url-with-slug"
case type
case dateGmt = "date-gmt"
case date, bookmarklet, mobile
case feedItem = "feed-item"
case fromFeedID = "from-feed-id"
case unixTimestamp = "unix-timestamp"
case format
case reblogKey = "reblog-key"
case slug
case isSubmission = "is-submission"
case likeButton = "like-button"
case reblogButton = "reblog-button"
case state
case noteCount = "note-count"
case tumblelog
case regularTitle = "regular-title"
case regularBody = "regular-body"
case tags
}
}
struct PostTumblelog: Codable {
let title, name: String
let cname: Bool
let url: String
let timezone: String
let avatarURL512, avatarURL128, avatarURL96, avatarURL64: String
let avatarURL48, avatarURL40, avatarURL30, avatarURL24: String
let avatarURL16: String
enum CodingKeys: String, CodingKey {
case title, name, cname, url, timezone
case avatarURL512 = "avatar_url_512"
case avatarURL128 = "avatar_url_128"
case avatarURL96 = "avatar_url_96"
case avatarURL64 = "avatar_url_64"
case avatarURL48 = "avatar_url_48"
case avatarURL40 = "avatar_url_40"
case avatarURL30 = "avatar_url_30"
case avatarURL24 = "avatar_url_24"
case avatarURL16 = "avatar_url_16"
}
}
struct TumblrJSONTumblelog: Codable {
let title, description, name, timezone: String
let cname: Bool
}
Значение JSON
var tumblr_api_read = {"tumblelog":{"title":"Timberland","description":"","name":"timberland","timezone":"US\/Eastern","cname":false,"feeds":[]},"posts-start":0,"posts-total":493,"posts-type":false,"posts":[{"id":"132163144823","url":"http:\/\/timberland.tumblr.com\/post\/132163144823","url-with-slug":"http:\/\/timberland.tumblr.com\/post\/132163144823\/publish-x-timberland-collaboration-coming","type":"photo","date-gmt":"2015-10-29 19:33:36 GMT","date":"Thu, 29 Oct 2015 15:33:36","bookmarklet":0,"mobile":0,"feed-item":"","from-feed-id":0,"unix-timestamp":1446147216,"format":"html","reblog-key":"tG24PRhC","slug":"publish-x-timberland-collaboration-coming","is-submission":false,"like-button":"<div class=\"like_button\" data-post-id=\"132163144823\" data-blog-name=\"timberland\" id=\"like_button_132163144823\"><iframe id=\"like_iframe_132163144823\" src=\"https:\/\/assets.tumblr.com\/assets\/html\/like_iframe.html?_v=66c22ab5319d742bca5762b8d18f9d06#name=timberland&post_id=132163144823&color=black&rk=tG24PRhC\" scrolling=\"no\" width=\"20\" height=\"20\" frameborder=\"0\" class=\"like_toggle\" allowTransparency=\"true\" name=\"like_iframe_132163144823\"><\/iframe><\/div>","reblog-button":"<a href=\"https:\/\/www.tumblr.com\/reblog\/132163144823\/tG24PRhC\" class=\"reblog_button\"style=\"display: block;width:20px;height:20px;\"><svg width=\"100%\" height=\"100%\" viewBox=\"0 0 21 21\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" xmlns:xlink=\"http:\/\/www.w3.org\/1999\/xlink\" fill=\"#000\"><path d=\"M5.01092527,5.99908429 L16.0088498,5.99908429 L16.136,9.508 L20.836,4.752 L16.136,0.083 L16.1360004,3.01110845 L2.09985349,3.01110845 C1.50585349,3.01110845 0.979248041,3.44726568 0.979248041,4.45007306 L0.979248041,10.9999998 L3.98376463,8.30993634 L3.98376463,6.89801007 C3.98376463,6.20867902 4.71892527,5.99908429 5.01092527,5.99908429 Z\"><\/path><path d=\"M17.1420002,13.2800293 C17.1420002,13.5720293 17.022957,14.0490723 16.730957,14.0490723 L4.92919922,14.0490723 L4.92919922,11 L0.5,15.806 L4.92919922,20.5103758 L5.00469971,16.9990234 L18.9700928,16.9990234 C19.5640928,16.9990234 19.9453125,16.4010001 19.9453125,15.8060001 L19.9453125,9.5324707 L17.142,12.203\"><\/path><\/svg><\/a>","note-count":"70","tumblelog":{"title":"Timberland","name":"timberland","cname":false,"url":"http:\/\/timberland.tumblr.com\/","timezone":"US\/Eastern","avatar_url_512":"https:\/\/66.media.tumblr.com\/avatar_571109f21077_512.png","avatar_url_128":"https:\/\/66.media.tumblr.com\/avatar_571109f21077_128.png","avatar_url_96":"https:\/\/66.media.tumblr.com\/avatar_571109f21077_96.png","avatar_url_64":"https:\/\/66.media.tumblr.com\/avatar_571109f21077_64.png","avatar_url_48":"https:\/\/66.media.tumblr.com\/avatar_571109f21077_48.png","avatar_url_40":"https:\/\/66.media.tumblr.com\/avatar_571109f21077_40.png","avatar_url_30":"https:\/\/66.media.tumblr.com\/avatar_571109f21077_30.png","avatar_url_24":"https:\/\/66.media.tumblr.com\/avatar_571109f21077_24.png","avatar_url_16":"https:\/\/66.media.tumblr.com\/avatar_571109f21077_16.png"},"photo-caption":"<b>Publish X Timberland Collaboration<\/b><br\/><br\/><p>Coming November 7: our latest collaboration with Publish Brand. Mark your calendars. <\/p><p>\n (via <a href=\"http:\/\/pco.lt\/1O3sTq4\">Publish X Timberland Collaboration<\/a>)\n <\/p>","width":520,"height":347,"photo-url-1280":"https:\/\/66.media.tumblr.com\/55f5b1f0f1076924a8f6221740f430b1\/tumblr_nwzyc074G11r6oj70o1_540.jpg","photo-url-500":"https:\/\/66.media.tumblr.com\/55f5b1f0f1076924a8f6221740f430b1\/tumblr_nwzyc074G11r6oj70o1_500.jpg","photo-url-400":"https:\/\/66.media.tumblr.com\/55f5b1f0f1076924a8f6221740f430b1\/tumblr_nwzyc074G11r6oj70o1_400.jpg","photo-url-250":"https:\/\/66.media.tumblr.com\/55f5b1f0f1076924a8f6221740f430b1\/tumblr_nwzyc074G11r6oj70o1_250.jpg","photo-url-100":"https:\/\/66.media.tumblr.com\/55f5b1f0f1076924a8f6221740f430b1\/tumblr_nwzyc074G11r6oj70o1_100.jpg","photo-url-75":"https:\/\/66.media.tumblr.com\/55f5b1f0f1076924a8f6221740f430b1\/tumblr_nwzyc074G11r6oj70o1_75sq.jpg","photos":[],"tags":["collab","timberland","publishbrand","timberlandxpublish"]}]};