Last active
October 14, 2020 08:39
-
-
Save andreconghau/4164bf992d0e6edccfc2d3aefd92974d to your computer and use it in GitHub Desktop.
Swift 5 Using SwiftJson to catch data Json without Decodable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/SwiftyJSON/SwiftyJSON | |
import Alamofire | |
import SwiftyJSON | |
classs ExampleSwiftyJson { | |
func jsonSwiftyJson() { | |
let user = "user" | |
let password = "password" | |
let credential = URLCredential(user: user, password: password, persistence: .forSession) | |
AF.request("https://api.github.com/search/issues?q=windows+label:bug+language:python+state:open&sort=created&order=asc") | |
.authenticate(with: credential) | |
.validate(statusCode: 200..<300) | |
.validate(contentType: ["application/json"]) | |
.responseData { response in | |
// debugPrint(response) | |
switch response.result { | |
case .failure(let error): | |
print(error) | |
case .success(let data): | |
do { | |
let jsonData = try JSON(data: data) | |
// print(jsonData) | |
let total_count = jsonData["total_count"].intValue | |
print("total_count: \(total_count)") | |
let user_1_login = jsonData["items"][0]["user"]["login"] | |
let user_2_login = jsonData["items"][1]["user"]["login"] | |
print(user_1_login) | |
print(user_2_login) | |
// Foreach if is Json . reference at: https://github.com/SwiftyJSON/SwiftyJSON#loop | |
for (key,subJson):(String, JSON) in jsonData["items"] { | |
print("#oder: \(key) - user: \(subJson["user"]["login"]) - avarta_url \(subJson["user"]["avatar_url"])") | |
} | |
} catch (let errSwiftyJson) { | |
print(errSwiftyJson) | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
total_count: 7114 | |
certik | |
#oder: 0 - user: certik - avarta_url https://avatars3.githubusercontent.com/u/20568?v=4 | |
#oder: 1 - user: sympy-issue-migrator - avarta_url https://avatars0.githubusercontent.com/u/6188475?v=4 | |
#oder: 2 - user: vks - avarta_url https://avatars2.githubusercontent.com/u/33460?v=4 | |
#oder: 3 - user: vks - avarta_url https://avatars2.githubusercontent.com/u/33460?v=4 | |
#oder: 4 - user: vks - avarta_url https://avatars2.githubusercontent.com/u/33460?v=4 | |
#oder: 5 - user: certik - avarta_url https://avatars3.githubusercontent.com/u/20568?v=4 | |
#oder: 6 - user: certik - avarta_url https://avatars3.githubusercontent.com/u/20568?v=4 | |
#oder: 7 - user: trevorc - avarta_url https://avatars2.githubusercontent.com/u/63377?v=4 | |
#oder: 8 - user: sympy-issue-migrator - avarta_url https://avatars0.githubusercontent.com/u/6188475?v=4 | |
#oder: 9 - user: imrehg - avarta_url https://avatars0.githubusercontent.com/u/38863?v=4 | |
#oder: 10 - user: imrehg - avarta_url https://avatars0.githubusercontent.com/u/38863?v=4 | |
#oder: 11 - user: imrehg - avarta_url https://avatars0.githubusercontent.com/u/38863?v=4 | |
#oder: 12 - user: Nick3C - avarta_url https://avatars3.githubusercontent.com/u/90254?v=4 | |
#oder: 13 - user: gak - avarta_url https://avatars3.githubusercontent.com/u/31338?v=4 | |
#oder: 14 - user: sympy-issue-migrator - avarta_url https://avatars0.githubusercontent.com/u/6188475?v=4 | |
#oder: 15 - user: hut3 - avarta_url https://avatars1.githubusercontent.com/u/134333?v=4 | |
#oder: 16 - user: nvaccessAuto - avarta_url https://avatars2.githubusercontent.com/u/14253915?v=4 | |
#oder: 17 - user: asmeurer - avarta_url https://avatars3.githubusercontent.com/u/71486?v=4 | |
#oder: 18 - user: nvaccessAuto - avarta_url https://avatars2.githubusercontent.com/u/14253915?v=4 | |
#oder: 19 - user: ipython - avarta_url https://avatars3.githubusercontent.com/u/230453?v=4 | |
#oder: 20 - user: tsukkee - avarta_url https://avatars3.githubusercontent.com/u/9748?v=4 | |
#oder: 21 - user: nvaccessAuto - avarta_url https://avatars2.githubusercontent.com/u/14253915?v=4 | |
#oder: 22 - user: nvaccessAuto - avarta_url https://avatars2.githubusercontent.com/u/14253915?v=4 | |
#oder: 23 - user: sqlalchemy-bot - avarta_url https://avatars2.githubusercontent.com/u/36047385?v=4 | |
#oder: 24 - user: nvaccessAuto - avarta_url https://avatars2.githubusercontent.com/u/14253915?v=4 | |
#oder: 25 - user: nvaccessAuto - avarta_url https://avatars2.githubusercontent.com/u/14253915?v=4 | |
#oder: 26 - user: andythigpen - avarta_url https://avatars2.githubusercontent.com/u/542263?v=4 | |
#oder: 27 - user: asmeurer - avarta_url https://avatars3.githubusercontent.com/u/71486?v=4 | |
#oder: 28 - user: fortable1999 - avarta_url https://avatars0.githubusercontent.com/u/748497?v=4 | |
#oder: 29 - user: nvaccessAuto - avarta_url https://avatars2.githubusercontent.com/u/14253915?v=4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment