1
在我的swift
應用程序中,我使用SwiftyJSON
和Alamofire
。如何解析alamofire返回json到Swift中的字符串數組?
我有回來從我的後端應用一個JSON:
{
"responses" : [
{
"labelAnnotations" : [
{
"mid" : "\/m\/01yrx",
"score" : 0.8735667499999999,
"description" : "cat"
},
{
"mid" : "\/m\/0l7_8",
"score" : 0.7697883,
"description" : "floor"
},
{
"mid" : "\/m\/01c34b",
"score" : 0.7577944,
"description" : "flooring"
},
{
"mid" : "\/m\/03f6tq",
"score" : 0.52875614,
"description" : "living room"
},
{
"mid" : "\/m\/01vq3",
"score" : 0.52516687,
"description" : "christmas"
}
]
}
]
}
我要構建的Strings
數組,其中包含上述各描述。 我試着用代碼來解析它:
{
case .success:
print("sukces")
if let jsonData = response.result.value {
let data = JSON(jsonData)
print(data)
if let responseData = data["responses"] as? JSON{
if let responseData2 = responseData["labelAnnotations"] as? JSON{
for userObject in responseData2 {
print(userObject["description"])
}
}
}
}
case .failure(let error):
print("fail")
print(error)
}
}
但線print(userObject)
返回空字符串。我如何顯示每個描述?只要我可以在控制檯中打印它,我會將它添加到我的數組中。
謝謝,效果不錯:) – user3766930