2016-04-13 24 views
1

SwiftyJSONでJSON値をループしてUIAlertControllerに表示しようとしています。SwiftyJSONでJSONをループする

if let errorVal = errorVal { 

var errorMessages = "" 

for (_,subJson):(String, JSON) in errorVal { 

    errorMessages = errorMessages + String(subJson) + "\n" 
} 

} 

errorVal戻っている:Alamofireを使用して、私のAPIコールで

私はその後、私のVCで、私はとのデータがあることを使用

if((responseData.result.value) != nil) { 

let json = JSON(responseData.result.value!) 
let token = json["api_token"].string 

response(token: token, errorVal: json) 

} else { 
    return 
} 

を使用して、いくつかのJSONデータを返すのです

{ 
    "email" : [ 
    "The email field is required." 
    ], 
    "password" : [ 
    "The password field is required." 
    ] 
} 

およびerrorMessages

[ 
"The email field is required." 
] 
[ 
"The password field is required." 
] 

しかし、私はerrorMessagesではなく、これを表示したい:

The email field is required 
The password field is required 

どのようにループJSON経由のみが値を得ることができますか?

var newMessage = String(subJson).stringByReplacingOccurrencesOfString("[", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil) 
    newMessage = newMessage.stringByReplacingOccurrencesOfString("]", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil) 
    errorMessages = errorMessages + newMessage + "\n" 

答えて

1

subJsonは、文字列の配列であることからは、最初のものを得る:

1

あなたはこのようなものを使用することができます。

if let errorVal = errorVal { 
    var errorMessages = "" 

    for (_,subJson):(String, JSON) in errorVal { 
     // Looks like subJson is an array, so grab the 1st element 
     let s = subJson[0].string 
     errorMessages = errorMessages + s + "\n" 
    } 

} 
関連する問題