How do I access a nested JSON(嵌套JSON) value using Alamofire and SwiftyJSON

http://stackoverflow.com/questions/35287836/how-do-i-access-a-nested-json-value-using-alamofire-and-swiftyjson

func getAPI() {
Alamofire.request(.GET, “http://quotes.rest/qod.json“, parameters: [“contents”: “quotes”])
// JSON response
.responseJSON { response in switch response.result {
case .Failure(let error):
// got an error in getting the data, need to handle it
print(“error calling GET, json response type :”)
// print alamofire error code
let statusCode = error.code
print(“error code json : (statusCode)”)
// print json response from server
if let data = response.data {
print(“Response data: (NSString(data: data, encoding: NSUTF8StringEncoding)!)”)
}
// print http status code plus error string
print(NSHTTPURLResponse.localizedStringForStatusCode(statusCode))
if let httpResponse : NSHTTPURLResponse = response.response {
print(“HTTP Response statusCode: (httpResponse.statusCode)”)
}
case .Success( _):
let statusCode = (response.response?.statusCode)!
print(“status code json : (statusCode)”)
print(“there is a response json”)
//print(value)
// parse the result as JSON, since that’s what the API provides and save datas as new user in coreData
guard let data = response.data else {
print(“Error parsing response data”)
return
}
let json = JSON(data: data)
// access first element of the array
if let postContent = json[“contents”][“quotes”][0][“quote”].string{
// deal with json
}
}
}

http://stackoverflow.com/questions/31715907/how-to-use-swiftyjson-on-nested-json-values

Here is a sample of the JSON:

{
userInfo: {
something: “abc”,
requestType: “GET”
},
results: [
{
username: “Jess”,
firstName: “Jessica”,
userImages: {
profile: [
{
userImageID: “6”,
filename: “user-07.jpg”
}
],
cover: [
{
userImageID: “15”,
filename: “user-07.jpg”
}
]
},
characters: [
{
userCharacterID: “8”,
characterName: “Amelia”,
}
]
}

var profileImage: String! = userDict[“userImages”][“profile”][0][“filename”].string

Alamofire.request(APIRequests.Router.Nearby(self.page)).responseJSON(){
(,,json,_) in
println(json)
if (json != nil){
var jsonObj = JSON(json!)

        if let userArray = jsonObj ["results"].array {

            for userDict in userArray {
                    var username: String! = userDict["username"].string
                    var firstName: String! = userDict["firstName"].string
                    var profileImage: String! = userDict["userImages"]["profile"][0]["filename"].string
                    var characterName: String! = userDict["characters"][0]["characterName"].string

                    var user = User(username: username, profileImage: profileImage, firstName: firstName, characterName: characterName)

                    self.users.append(user)
                }
            }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值