使用Swifty JSONparsing数组
这是我的JSONparsing(示例):
[ { "id": 1, "name": "Team name", "shower": { "id": 1, "status": 1, "startLocation": { "id": 1, "name": "abc 16" } } }, { "id": 2, "name": "Team name", "shower": { "id": 2, "status": 1, "startLocation": { "id": 1, "name": "efg 16" } } } ]
- 粘贴这个json查看器来轻松查看。
正如你所看到的,这是一个数组(团队)。 我需要让每个球队都做些什么。
经过多次尝试,我尝试使用SwiftyJSON ,因为我认为它会更容易。 但是,它没有为我工作。
这是我试过的:
let array = JSON(response) // print each subJSON in array for team in array.arrayValue { print(team) }
但循环不起作用。 它根本不会进入循环。 也许它不明白,我的JSON是一个数组。
我可以在debugging器中看到数组对象。 它看起来像这样:
我怎样才能得到这些子JSON?
谢谢。
我想你应该使用
let array = JSON(parseJSON: response)
代替
let array = JSON(response)
SwiftyJSON包含将JSONstringparsing为JSON对象的方法,请检查文档
/** Parses the JSON string into a JSON object - parameter json: the JSON string - returns: the created JSON object */ public init(parseJSON jsonString: String) { if let data = jsonString.data(using: .utf8) { self.init(data) } else { self.init(NSNull()) } } /** Creates a JSON from JSON string - parameter string: Normal json string like '{"a":"b"}' - returns: The created JSON */ @available(*, deprecated: 3.2, message: "Use instead `init(parseJSON: )`") public static func parse(json: String) -> JSON { return json.data(using: String.Encoding.utf8) .flatMap{ JSON(data: $0) } ?? JSON(NSNull()) }
或者可以将子串转换成子对象
Swift 3:
let dataFromString = response.data(using: .utf8) let jsonArray = JSON(data: dataFromString!)
在下面的例子中,我将队名保存在一个数组中。 我testing过了。
var names = [String]() if let array = json.array { for i in 0..<array.count { let name = array[i]["name"] names.append(name.stringValue) } } print(names) // ["Team name", "Team name"]
没有SwiftyJSON
以下是有效的JSON
data.json文件
[{ "id": 1, "name": "Team name", "shower": { "id": 1, "status": 1, "startLocation": { "id": 1, "name": "abc 16" } } }, { "id": 2, "name": "Team name", "shower": { "id": 2, "status": 1, "startLocation": { "id": 1, "name": "efg 16" } } }]
以下是阅读你的JSON的代码。
if let path = Bundle.main.path(forResource: "data", ofType: "json") { let url = URL(fileURLWithPath: path) do { let data = try Data(contentsOf: url) if let jsonArray = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSArray { for (_, item) in jsonArray.enumerated() { let itemDict = item as! NSDictionary let id = itemDict["id"] as! Int let name = itemDict["name"] as! String let shower = itemDict["shower"] as! NSDictionary let showerId = shower["id"] as! Int let showerStatus = shower["status"] as! Int let startLocation = shower["startLocation"] as! NSDictionary let startLocationId = startLocation["id"] as! Int let startLocationName = startLocation["name"] as! String } } } catch { print("Error: \(error.localizedDescription)") } }
这是什么对我有用:
// Convert JSON to Array func JSONToArray(_ json: String) -> Array<Any>? { if let data = json.data(using: String.Encoding.utf8) { do { return try JSONSerialization.jsonObject(with: data, options: []) as? Array } catch let error as NSError { print(error) } } return nil }
使用这个函数后,我可以循环通过子JSONs。
谢谢。