내 API 쿼리가 배열에 여러 값을 반환하고 있습니다 (아래 참조). 추가 기능에 사용해야합니다. 중첩 된 값 60을 어떻게 검색합니까?
내 코드는 예를 들어 let uid = value["uid"] as? String
uid와 이름을 반환하는 데 잘 작동합니다. 그러나 let rest = value["field_resting_bpm"] as? String
그것은 nil을 반환합니다.
중첩 된 값 '60'을 반환하도록이 줄을 수정하려면 어떻게해야합니까? :
let rest = value["field_resting_bpm"] as? String'
API 쿼리는 다음과 같습니다.
let credentialData = "\(user):\(password)".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentialData.base64EncodedStringWithOptions([])
let headers = ["Authorization": "Basic \(base64Credentials)"]
var checkUserEndpoint : String = " https://example.site/ios1/user/1.json "
Alamofire.request(.GET, checkUserEndpoint, parameters: nil, headers: headers, encoding: .JSON)
.responseJSON { response in
guard response.result.error == nil else {
print("ran alamofire")
//got an error in getting the data, need to handle it
print("error calling GET")
print(response.result.error!)
return
}
guard let value = response.result.value else {
print("no result data received when calling GET")
return
}
// return UID
print("ran alamofire")
var datareturned = JSON(value)
print("VALUE: \(value)")
내 콘솔에 이것을 반환합니다.
uid = 1;
name = "Guest User";
"field_resting_bpm" = {
und = (
{
value = 60;
}
);
업데이트 :이 필드는 Drupal 사용자 계정 설정의 정수입니다. 테스트를 위해 field_rest_bpm_string이라는 텍스트 필드를 추가했습니다 (문자열을 반환해야 함).
사이트의 json은 다음을 반환합니다.
"field_resting_bpm": { "und": [{ "value": "60"}]}, "field_rest_bpm_string": { "und": [{ "value": "65", "format": null, "safe_value" : "65"}]}
참고 : 코드는 Swift 3에 있지만 swift 2에서 쉽게 변환 할 수 있습니다. SwiftyJSON
버전 코드는 2와 3에서 동일합니다.
당신이 사용 SwiftyJSON
한다면 상황은 훨씬 더 간단합니다
let value = datareturned["field_resting_bpm"]["und"][0]["value"].stringValue
print(value)
사용하지 않는 경우 SwiftyJSON
let rest = value["field_resting_bpm"] as? NSDictionary
let und = (rest?.value(forKey: "und") as! NSArray).object(at: 0) as! NSDictionary
let yourValue = und.value(forKey: "value") as! String
이 같은.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다