如何将 alamofire return json 解析为 Swift 中的字符串数组?
how can I parse alamofire return json to the array of strings in Swift?
在我的 swift
应用程序中,我使用 SwiftyJSON
和 Alamofire
。
我有一个 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)
returns 为空字符串。如何显示每个描述?一旦我可以在控制台中打印它,我就会将它添加到我的数组中。
检查字典值是否属于 JSON
类型似乎是这里的问题,因为 SwiftyJSON
所做的一切都是为了避免使用 as? ...
.[= 进行类型检查的麻烦21=]
我对图书馆不熟悉,但我想你所要做的就是:
(假设 response.result.value
returns 一个字典,因为你使用了 .responseJSON
方法,比如 Alamofire.request(...).responseJSON(...)
否则你将不得不做 JSON(data: [= 17=].data)
如果你调用了 .response(...)
。)
Alamofire.request(...).responseJSON { response in
if let dictionary = response.result.value {
let JSONData = JSON(dictionary)
let userObjects = JSONData["responses"][0]["labelAnnotations"].arrayValue.map(
{ [=10=]["description"].stringValue }
)
}
}
在我的 swift
应用程序中,我使用 SwiftyJSON
和 Alamofire
。
我有一个 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)
returns 为空字符串。如何显示每个描述?一旦我可以在控制台中打印它,我就会将它添加到我的数组中。
检查字典值是否属于 JSON
类型似乎是这里的问题,因为 SwiftyJSON
所做的一切都是为了避免使用 as? ...
.[= 进行类型检查的麻烦21=]
我对图书馆不熟悉,但我想你所要做的就是:
(假设 response.result.value
returns 一个字典,因为你使用了 .responseJSON
方法,比如 Alamofire.request(...).responseJSON(...)
否则你将不得不做 JSON(data: [= 17=].data)
如果你调用了 .response(...)
。)
Alamofire.request(...).responseJSON { response in
if let dictionary = response.result.value {
let JSONData = JSON(dictionary)
let userObjects = JSONData["responses"][0]["labelAnnotations"].arrayValue.map(
{ [=10=]["description"].stringValue }
)
}
}