我有一個JSON
字符串,並希望從它獲得一些值。我正在使用SwiftyJSON
從JSON字符串中提取一些值。但不知道爲什麼我從中獲得空值。如何從這個JSON字符串中使用SwiftyJSON
檢索所有值,如version, ephemeralPublicKey, transactionId
等?如何使用SwiftyJSON從Swift中獲取JSON字符串的值?
代碼:
let decryptedPaymentData:NSString! = NSString(data: encryptedPaymentData, encoding: NSUTF8StringEncoding)
print("decryptedPaymentData = \(decryptedPaymentData)")
//extract values here
let decryptedJsonStr = JSON(decryptedPaymentData)
let version = decryptedJsonStr["version"].stringValue
print("version = \(version)")
輸出:
decryptedPaymentData = {"version":"EC_v1",
"header":{
"ephemeralPublicKey":"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE1AhSWKm/KoXMmRU/dm825c8M3HZUYH/X8SH2zvzLgF9AN0ifWNH6KMLdRXhYSbnhVv0eDz+cJhf0I6o3oSc1bg==","publicKeyHash":"y9Gi+xcoSJjzegmkLQJ1KB7oehqavQom5EDwNCv+nHE=","transactionId":"7f83d3eff0de5d1313b6689d274d29dd7f71fe3763829f488ff4ac23a3dd40c5"}
}
version =
解輸出:
let decryptedJsonStr = JSON.parse(decryptedPaymentData as String)
let version = decryptedJsonStr["version"].stringValue
let epk = decryptedJsonStr["header"]["ephemeralPublicKey"].stringValue
print("version = \(version)")
print("epk = \(epk)")
version = EC_v1
epk=MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEoyXCcna7anfdPXVJcRFF6+SOgCgCqRqcdx/p0YrTy3nUU1LQ977EwuddRvnHKi5WemLnWIf3AzPDGLMY1L2Pog==
如果這沒有幫助http://roadfiresoftware.com/2015/10/how-to-parse-json-with-swift-2/,請張貼並輸出,至少從print()函數,你得到。 – pedrouan