如何使用 SWIFT3 解析此 JSON [重复]
Posted
技术标签:
【中文标题】如何使用 SWIFT3 解析此 JSON [重复]【英文标题】:How to parse this JSON using SWIFT3 [duplicate] 【发布时间】:2017-02-10 13:25:32 【问题描述】:这里我想通过 url 解析 JSON。这是 url 上可用的实际 JSON 数据。所以我需要解析它并使用 Alamofire 在我的应用程序中读取。但我做不到。
我的网址中的 JSON 数据。
["date":"2017-01-15","USDARS":"15.839023","USDCLP":"659.200012","USDVEF":"9.974801","USDCOP":"2943","USDPEN":"3.359497","USDBOB":"6.859692","USDPYG":"5780.799805","USDUYU":"28.660097","USDGTQ":"7.533499","USDCUP":"0.999845","USDCRC":"547.599976","USDHTG":"64.129997","USDJMD":"128.309998","USDDOP":"46.230097","USDETB":"22.459999","USDNGN":"314.999972","USDEGP":"18.900947","USDCDF":"1227.999527","USDTZS":"2216.999869","USDKES":"103.650002","USDSDG":"6.4077",
"date":"2017-01-16","USDARS":"15.849011","USDCLP":"662.099976","USDVEF":"9.974797","USDCOP":"2936.800049","USDPEN":"3.367975","USDBOB":"6.860284","USDPYG":"5733.700195","USDUYU":"28.749899","USDGTQ":"7.545501","USDCUP":"0.999646","USDCRC":"547.099976","USDHTG":"64.129997","USDJMD":"128.179993","USDDOP":"46.230123","USDETB":"22.39805","USDNGN":"313.999947","USDEGP":"18.898647","USDCDF":"1225.999725","USDTZS":"2221.999959","USDKES":"103.650002","USDSDG":"6.407702",
"date":"2017-01-17","USDARS":"15.882954","USDCLP":"656.299988","USDVEF":"9.974798","USDCOP":"2914","USDPEN":"3.3392","USDBOB":"6.860283","USDPYG":"5771.000017","USDUYU":"28.680621","USDGTQ":"7.533498","USDCUP":"0.999979","USDCRC":"547.080017","USDHTG":"64.120003","USDJMD":"128.580002","USDDOP":"46.200001","USDETB":"22.397891","USDNGN":"314.999989","USDEGP":"18.649708","USDCDF":"1225.000306","USDTZS":"2227.000108","USDKES":"103.750025","USDSDG":"6.407696"]
如何在 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.
请帮助我。我是新手。
【问题讨论】:
为什么有两个if let arr
?不要使用NSDictionary
,使用 Swift 类型。它通过了第一次测试吗?如果是,if let main = arr["main"] as? [[String:AnyObject]]
(字典数组)应该是下一个合乎逻辑的步骤。然后let val1 = main[0]["USDARS"] as? String
?
你好,它不起作用
什么不起作用?您知道您正在执行多个“if”测试,但其中任何一个都可能失败?究竟是哪一个(您可以添加一个“其他”案例)?你连第一个都通过了吗?
使用github.com/SwiftyJSON/SwiftyJSON。在这方面处理 JSON 超级容易。
使用@iAviator 提到的 SwiftJson,使用该库处理 JSON 超级容易。
【参考方案1】:
我推荐使用EVReflection,你必须创建2个模型,然后你可以轻松管理结果。
型号:
class RootElementModel : EVObject
var main : [MainElement];
class MainElementModel : EVObject
var date : String = "";
var USDARS : CGFloat = 0;
var USDCLP : CGFloat = 0;
var USDSDG : CGFloat = 0;
然后你可以使用如下:
Alamofire.request("url").responseString
response in
let resString = response.result.value
let items = RootElementModel(json : resString).main
print("first item USDARS = \(items[0].USDARS)")
更多使用示例和安装教程可以阅读Official Documentation。
【讨论】:
【参考方案2】:Alamofire.request(url).responseJSON(completionHandler:
response in
self.parseData(JSONData: response.data!)
)
typealias JSONStandard = [String : AnyObject]
func parseData(JSONData : Data)
do
var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard
if let items = readableJSON["main"] as? [JSONStandard]
for i in 0..<items.count
let item = items[i]
if let USDARS = item["USDARS"]
print(USDARS)
catch
print(error)
要打印特定值,可以通过两种方式进行:
每次搜索时都发出一个新的 Alamofire 请求:
let whatToSearchFor: String = "13-02-2017"
Alamofire.request(url).responseJSON(completionHandler:
response in
self.parseData(JSONData: response.data!)
)
typealias JSONStandard = [String : AnyObject]
func parseData(JSONData : Data)
do
var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard
if let items = readableJSON["main"] as? [JSONStandard]
for i in 0..<items.count
let item = items[i]
guard item["date"] != nil else print("nil"); return
guard item["USDARS"] != nil else print("nil"); return
if item["date"] == whatToSearchFor
print(item["USDARS"])
catch
print(error)
创建一个包含所有值的字典,然后打印搜索到的字典:
let dict = [String : String]
Alamofire.request(url).responseJSON(completionHandler:
response in
self.parseData(JSONData: response.data!)
)
typealias JSONStandard = [String : AnyObject]
func parseData(JSONData : Data)
do
var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard
if let items = readableJSON["main"] as? [JSONStandard]
for i in 0..<items.count
let item = items[i]
guard item["date"] != nil else print("nil"); return
guard item["USDARS"] != nil else print("nil"); return
let date = item["date"]
let USDARS = item["USDARS"]
dict[date] = USDARS
catch
print(error)
let whatToSearchFor: String = "13-02-2017"
print(dict[whatToSearchFor])
在 JSON 中没有“main”后编辑:
let whatToSearchFor: String = "13-02-2017"
Alamofire.request(url).responseJSON(completionHandler:
response in
self.parseData(JSONData: response.data!)
)
typealias JSONStandard = [String : AnyObject]
func parseData(JSONData : Data)
do
var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard
if let items = readableJSON
for i in 0..<items.count
let item = items[i]
guard item["date"] != nil else print("nil"); return
guard item["USDARS"] != nil else print("nil"); return
if item["date"] == whatToSearchFor
print(item["USDARS"])
catch
print(error)
或者:
let dict = [String : String]
Alamofire.request(url).responseJSON(completionHandler:
response in
self.parseData(JSONData: response.data!)
)
typealias JSONStandard = [String : AnyObject]
func parseData(JSONData : Data)
do
var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard
if let items = readableJSON
for i in 0..<items.count
let item = items[i]
guard item["date"] != nil else print("nil"); return
guard item["USDARS"] != nil else print("nil"); return
let date = item["date"]
let USDARS = item["USDARS"]
dict[date] = USDARS
catch
print(error)
let whatToSearchFor: String = "13-02-2017"
print(dict[whatToSearchFor])
基本上main
消失了,as? [JSONStandard]
也消失了,因为readableJSON
被定义为JSONStandard
。
【讨论】:
您好,如果我想按日期打印,例如:13-02-2017 USDRS 值 我应该如何打印具有货币价值的特定日期。 这里打印所有值,由于 for 循环 更新了答案。 另外,添加了对 nil 值的检查。以上是关于如何使用 SWIFT3 解析此 JSON [重复]的主要内容,如果未能解决你的问题,请参考以下文章
JSON数据未在swift3中使用Alamofire进行解析
如何使用 Alamofire 在 Swift 3 中解析 JSON?
试图将 JSON 解析为 swift3 并显示到 UILabel