Ios 如何在Swift 3.0中使用Alamofire解析JSON,而无需任何第三方库
这里我想通过url解析JSON。这是url上可用的实际JSON数据。所以我需要解析它并使用Alamofire在我的应用程序中读取。但是我做不到 我的url中的JSON数据Ios 如何在Swift 3.0中使用Alamofire解析JSON,而无需任何第三方库,ios,json,swift,swift3,Ios,Json,Swift,Swift3,这里我想通过url解析JSON。这是url上可用的实际JSON数据。所以我需要解析它并使用Alamofire在我的应用程序中读取。但是我做不到 我的url中的JSON数据 { "main": [ { "date": "2017-01-11", "USDARS": "15.8302", "USDCLP": "670.400024", "USDSDG": "6.407695" },
{
"main": [
{
"date": "2017-01-11",
"USDARS": "15.8302",
"USDCLP": "670.400024",
"USDSDG": "6.407695"
},
{
"date": "2017-01-12",
"USDARS": "15.804999",
"USDCLP": "661.599976",
"USDSDG": "6.407697"
},
{
"date": "2017-01-13",
"USDARS": "15.839041",
"USDCLP": "659.200012",
"USDSDG": "6.407704"
},
{
"date": "2017-01-14",
"USDARS": "15.839041",
"USDCLP": "659.200012",
"USDSDG": "6.407704"
}
]
}
如何在swift 3.0中使用Alamofire阅读
下面是我试图通过url解析上述JSON数据的内容
Alamofire.request("myurl") .responseJSON { response in
print("In Alamofire")
if let arr = response.result.value as? [String:AnyObject]
{
if let arr = response.result.value as? [NSDictionary]
{
let val1 = (arr["main"]["USDARS"] as? String)
print(val1)
//It does not print any thing.
}
}
}
请帮帮我。我是新手。顶级json是
[String:Any]
,main是一个数组,即[[String:String]]
Alamofire.request("myurl") .responseJSON { response in
if let result = response.result.value as? [String:Any],
let main = result["main"] as? [[String:String]]{
// main[0]["USDARS"] or use main.first?["USDARS"] for first index or loop through array
for obj in main{
print(obj["USDARS"])
print(obj["date"])
}
}
}
您应该使用,这是一个用于json解析的库,对Alamofire非常有用
在您的情况下,您可以使用swiftyJson执行以下操作:
//Array & Dictionary
var jsonArray: JSON = [
"main": ["date": "2017-01-11", "USDARS": "15.8302"]
]
let dateString = jsonArray["main"][0]["date"].string
print(dateString) = "2017-01-11"
请尝试arr[“main”][0][“USDARS”],因为“main”是一个对象数组。几天前您没有发布过同样的问题吗?我不记得您在前面的评论或问题之后对代码做了任何更改。我以前报告过:如果让arr=response.result.value为,为什么
?[字符串:AnyObject]
然后如果让arr=response.result.value为?[NSDictionary]
这没有意义,您应该更喜欢Swift类型而不是NSStuff。是的,它会打印“USDAR”的所有值,但我应该如何只打印“obj”的特定值或最后一个值作为第一个值。。您只需通过main.first?[“USDARS”]
即可获得。。或者给出像main[0][“USDARS”]
ok这样的索引,如果我想得到特定日期的值,那么我应该怎么做呢?使用特定的键。。对于日期obj[“日期”]
。。对于USDSDGobj[“USDSDG”]
很高兴它有帮助:)不鼓励只使用代码的答案,因为它们不会为未来的读者提供太多信息。请对您所写的内容提供一些解释
@IBAction func btnget(_ sender: UIButton) {
let urlpath : String = "http://202.131.123.211/UdgamApi_v4/App_Services/UdgamService.asmx/GetAllTeacherData?StudentId=2011111"
let url = URL(string: urlpath)
var urlrequest = URLRequest(url: url!)
urlrequest.httpMethod = "GET"
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: urlrequest) { (data, response, error) in
do {
guard let getResponseDic = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: AnyObject] else {
print("error trying to convert data to JSON")
return
}
// now we have the todo, let's just print it to prove we can access it
print(getResponseDic as NSDictionary)
let dic = getResponseDic as NSDictionary
let msg = dic.object(forKey: "message")
print(dic)
//let arrObj = dic.object(forKey: "teacherDetail") as! NSArray
//print(arrObj)
//let arr = dic.value(forKey: "TeacherName")as! NSArray
print(((dic.value(forKey: "teacherDetail") as! NSArray).object(at: 0) as! NSDictionary).value(forKey: "TeacherName") as! String)
// the todo object is a dictionary
// so we just access the title using the "title" key
// so check for a title and print it if we have one
// print("The title is: " + todoTitle)
} catch {
print("error trying to convert data to JSON")
return
}
} task.resume()
}