Use Swifty JSON to parse an array

This is my json for parsing (example):

[ { "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" } } } ] 

as you can see, this is an array of (commands). I need to get every team and do something with it.

After many attempts, I tried using SwiftyJSON , because I thought it would be easier. But that did not work for me.

This is what I tried:

 let array = JSON(response) // print each subJSON in array for team in array.arrayValue { print(team) } 

But the loop is not working. He does not enter the loop at all. Maybe he does not understand that my json is an array.

I see an array object in the debugger. It looks like this:

enter image description here

How can I get these sub-JSON?

Thanks.

-one
json ios swift swifty-json
source share
5 answers

I think you should use

 let array = JSON(parseJSON: response) 

instead

 let array = JSON(response) 
+1
source share

SwiftyJSON contains methods for parsing a JSON string in a JSON object, check the documentation

 /** 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()) } 

or, alternatively, you can convert the son string to a son object, for example

Swift 3:

 let dataFromString = response.data(using: .utf8) let jsonArray = JSON(data: dataFromString!) 
0
source share

In the following example, I store the command names in an array. I tested it.

 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"] 
0
source share

Without SwiftyJSON

The following is valid JSON

data.json File

 [{ "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" } } }] 

Below is the code to read your 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)") } } 
-one
source share

this is what worked for me:

  // 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 } 

After using this function, I could loop into sub JSON.

Thanks.

-one
source share

All Articles