使用字典和字典数组进行Swift JSON解析

编程入门 行业动态 更新时间:2024-10-23 14:34:18
本文介绍了使用字典和字典数组进行Swift JSON解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是使用Swift语言进行iOS开发的初学者.我有一个包含以下数据的JSON文件.

I am a beginner in iOS development with Swift language. I have a JSON file contains the data as below.

{ "success": true, "data": [ { "type": 0, "name": "Money Extension", "bal": "72 $", "Name": "LK_Mor", "code": "LK_Mor", "class": "0", "withdraw": "300 $", "initval": "1000 $" }, { }, { }, ] }

我想解析此文件,并且必须返回包含JSON文件中数据的字典.这是我写的方法.

I want to parse this file and have to return the dictionary which contain the data in the JSON file. This is the method I wrote.

enum JSONError: String, ErrorType { case NoData = "ERROR: no data" case ConversionFailed = "ERROR: conversion from JSON failed" } func jsonParserForDataUsage(urlForData:String)->NSDictionary{ var dicOfParsedData :NSDictionary! print("json parser activated") let urlPath = urlForData let endpoint = NSURL(string: urlPath) let request = NSMutableURLRequest(URL:endpoint!) NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in do { guard let dat = data else { throw JSONError.NoData } guard let dictionary: NSDictionary = try NSJSONSerialization.JSONObjectWithData(dat, options:.AllowFragments) as? NSDictionary else { throw JSONError.ConversionFailed } print(dictionary) dicOfParsedData = dictionary } catch let error as JSONError { print(error.rawValue) } catch { print(error) } }.resume() return dicOfParsedData }

当我修改此方法以返回字典时,它总是返回nil.如何修改此方法.

When I modify this method to return a dictionary, it always return nil. How can I modify this method.

推荐答案

您不能return进行异步任务.您必须改用回调.

You can not return for an asynchronous task. You have to use a callback instead.

添加这样的回调:

completion: (dictionary: NSDictionary) -> Void

解析器方法签名:

func jsonParserForDataUsage(urlForData: String, completion: (dictionary: NSDictionary) -> Void)

并在您要返回"的数据可用的地方调用补全:

and call the completion where the data you want to "return" is available:

func jsonParserForDataUsage(urlForData: String, completion: (dictionary: NSDictionary) -> Void) { print("json parser activated") let urlPath = urlForData guard let endpoint = NSURL(string: urlPath) else { return } let request = NSMutableURLRequest(URL:endpoint) NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in do { guard let dat = data else { throw JSONError.NoData } guard let dictionary = try NSJSONSerialization.JSONObjectWithData(dat, options:.AllowFragments) as? NSDictionary else { throw JSONError.ConversionFailed } completion(dictionary: dictionary) } catch let error as JSONError { print(error.rawValue) } catch let error as NSError { print(error.debugDescription) } }.resume() }

现在,您可以在尾随闭包中使用此方法来获取返回"值:

Now you can use this method with a trailing closure to get the "returned" value:

jsonParserForDataUsage("http...") { (dictionary) in print(dictionary) }

更多推荐

使用字典和字典数组进行Swift JSON解析

本文发布于:2023-11-26 03:09:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632393.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字典   数组   JSON   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!