Alamofire.request(NEWS_FEED_URL).responseJSON { response in
guard let newsResponse = response.result.value else{
print("Error: \(response.result.error)")
return
}
print("JSON: \(newsResponse)")
let localArray:Array = [newsResponse]
print("Local Array:", localArray)
}
I am using alamofire to fetch data from server and trying to write parser on the response data. When I fetch response.result.value from alamofire and print it is working fine. But as soon as I put response.result.value in my local array it gives
[<__NSArrayI 0x6080000bcf80>(
{
category = World;
})]
and so on as result (Console). Also I am unable to convert response value of type 'Any' to 'Array'. Please help me how to add response.result.value in local array of type 'Array'. Can someone explain something about type casting on alamofire in brief?
等等(控制台)。此外,我无法将“Any”类型的响应值转换为“Array”。请帮助我如何在“Array”类型的本地数组中添加response.result.value。有人能简单解释一下关于alamofire上的类型转换吗?
2 个解决方案
#1
1
You need to simply cast the result of response.result.value
to Array of Dictionary.
您只需将response.result.value的结果转换为Array of Dictionary。
guard let newsResponse = response.result.value as? [[String:String]] else{
print("Error: \(response.result.error)")
return
}
print("JSON: \(newsResponse)")
let localArray = newsResponse
//Access the localArray
print("Local Array:", localArray)
//Now to get the desired value from dictionary try like this way.
let dic = localArray[0]
let title = dic["title"] ?? "Default Title"
let link = dic["link"] as? "Default link"
//... get other value same way.
Note : Your response is already an Array
so don't make array from it by adding it inside another array.
注意:您的响应已经是一个数组,因此不要通过在另一个数组中添加数组来生成数组。
#2
1
There is problem in responseJOSN
result because result may not correct JSON format to correct use [[String:Any]]
for formating JSON
responseJOSN结果存在问题,因为结果可能无法更正JSON格式以正确使用[[String:Any]]来形成JSON
Alamofire.request(NEWS_FEED_URL).responseJSON { response in
guard let newsResponse = response.result.value as? [[String:Any]] else{
print("Error: \(response.result.error)")
return
}
print("JSON: \(newsResponse)")
let firstRow = newResponse[0]
let categoryValue = firstRow["category"] ?? "Default Category"
}
#1
1
You need to simply cast the result of response.result.value
to Array of Dictionary.
您只需将response.result.value的结果转换为Array of Dictionary。
guard let newsResponse = response.result.value as? [[String:String]] else{
print("Error: \(response.result.error)")
return
}
print("JSON: \(newsResponse)")
let localArray = newsResponse
//Access the localArray
print("Local Array:", localArray)
//Now to get the desired value from dictionary try like this way.
let dic = localArray[0]
let title = dic["title"] ?? "Default Title"
let link = dic["link"] as? "Default link"
//... get other value same way.
Note : Your response is already an Array
so don't make array from it by adding it inside another array.
注意:您的响应已经是一个数组,因此不要通过在另一个数组中添加数组来生成数组。
#2
1
There is problem in responseJOSN
result because result may not correct JSON format to correct use [[String:Any]]
for formating JSON
responseJOSN结果存在问题,因为结果可能无法更正JSON格式以正确使用[[String:Any]]来形成JSON
Alamofire.request(NEWS_FEED_URL).responseJSON { response in
guard let newsResponse = response.result.value as? [[String:Any]] else{
print("Error: \(response.result.error)")
return
}
print("JSON: \(newsResponse)")
let firstRow = newResponse[0]
let categoryValue = firstRow["category"] ?? "Default Category"
}