Swift 3 Parse JSON into UITableView using URLSession - json

I am trying to parse JSON using URLSession and without using Alamofire or anything else.
I just want to take the JSON and put it into a UITableView.
I am trying to piece together what I learned from learning how to Parse JSON using Alamofire with what I can find on google. Many of the answers on youtube or Stack etc use NS for everything..NSURL, NSDictionary, etc etc..Or are just typing code without explaining what/why.
I THINK I am almost there, but I need help understanding what I have left to do.
SO.
I Allowed arbitrary loads in the plst
In a Swift File I have the following
class Potter {
private var _title: String!
private var _author: String!
private var _imageURL: String!
let POTTER_URL = "http://de-coding-test.s3.amazonaws.com/books.json"
var title: String {
if _title == nil {
_title = ""
}
return _title
}
var author: String {
if _author == nil {
_author = ""
}
return _author
}
var imageURL: String {
if _imageURL == nil {
_imageURL = ""
}
return _imageURL
}
func downloadJSON() {
let url = URL(string: POTTER_URL)
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print("Error")
} else {
if let content = data {
do {
if let jDict = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as? Dictionary<String, AnyObject> {
if let title = jDict["title"] as? String {
self._title = title.capitalized
}
if let author = jDict["author"] as? String {
self._author = author.capitalized
}
if let imgURL = jDict["imageURL"] as? String {
self._imageURL = imgURL
}
}
}
catch {
}
}
}
}
task.resume()
}
}
In my Main.Storyboard I added the tableview and set up all the UI, and in my ViewController I have set up the tableview delegates.
I created a property of
var potters = [Potter]()
I am stuck now on how to I populate this array, and how do I set up the proper threading

First of all your model is insane pretty weird.
In Swift never use backed private variables to get read/only properties. And never declare properties as implicit unwrapped optional because you are too lazy to write an initializer.
The entire model can be reduced to
class Potter {
let title, author, imageURL: String
init(title: String, author: String, imageURL : String) {
self.title = title
self.author = author
self.imageURL = imageURL
}
}
If you would use a struct, it's even
struct Potter {
let title, author, imageURL: String
}
because you get the memberwise initializer for free.
Secondly, put the method downloadJSON() out of the model and put it in the controller and call it in viewDidLoad().
In the controller declare the download URL and the data source array
let POTTER_URL = "http://de-coding-test.s3.amazonaws.com/books.json"
var books = [Potter]()
Your method downloadJSON() cannot work because the JSON object is an array ([]), not a dictionary ({}). You need a loop to iterate thru the items, get the values, create a Potter item respectively and append it to the data source. If a value does not exist, an empty string is assigned. Finally reload the table view on the main thread.
func downloadJSON() {
let url = URL(string: POTTER_URL)
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print("DataTask error", error!)
} else {
do {
if let bookData = try JSONSerialization.jsonObject(with: data!) as? [[String:String]] {
books.removeAll() // clear data source array
for book in bookData {
let title = book["title"] ?? ""
let author = book["author"] ?? ""
let imgURL = book["imageURL"] ?? ""
books.append(Potter(title: title, author: author, imageURL: imgURL))
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
catch {
print("Serialization error", error)
}
}
}
task.resume()
}
Two notes:
The standard JSON dictionary in Swift 3 is [String:Any], in this particular case it's even [String:String].
.mutableContainers is useless if the containers are only read and useless in Swift anyway because the object cannot be casted to NSMutableArray / -Dictionary and you get mutability for free using a variable.

The web services returns an array of objects: [Dictionary<String, AnyObject>].
It will be easier if you create a init method with a dictionary as parameter.
The downloadJSON is an async task, using completionHandler is the best way. And if you want to place the downloadJSON in the Potter class, it should be a static function.
Final, you should handle the result like this:
Potter.downloadJSON { potters in
self.potters = potters
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
The final code:
class ViewController: UIViewController {
var potters = [Potter]()
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
Potter.downloadJSON { potters in
self.potters = potters
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return potters.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!
let potter = potters[indexPath.row]
cell.textLabel?.text = potter.title
cell.detailTextLabel?.text = potter.author
return cell
}
}
class Potter {
private var _title: String!
private var _author: String!
private var _imageURL: String!
static let POTTER_URL = "http://de-coding-test.s3.amazonaws.com/books.json"
var title: String {
if _title == nil {
_title = ""
}
return _title
}
var author: String {
if _author == nil {
_author = ""
}
return _author
}
var imageURL: String {
if _imageURL == nil {
_imageURL = ""
}
return _imageURL
}
init(dict: Dictionary<String, AnyObject>) {
self._title = dict["title"] as? String
self._imageURL = dict["imageURL"] as? String
self._author = dict["author"] as? String
}
class func downloadJSON(completion: #escaping (_ potters: [Potter]) -> Void) {
let url = URL(string: POTTER_URL)
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil {
print("Error")
} else {
if let content = data {
do {
if let jArray = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as? [Dictionary<String, AnyObject>] {
var potters = [Potter]()
for jDict in jArray {
let potter = Potter(dict: jDict)
potters.append(potter)
}
completion(potters)
}
}
catch {
}
}
}
}
task.resume()
}
}

The method downloadJSON() should be implemented in the ViewController since it is returning the array of Potter data. Then in the URLSession response you should create one array which will be act as the tableview datasource. (i.e self.arrTableData = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as? [[String : AnyObject]])
Then in for the tableView
func tableView(_ tableView: UITableView, numberOfRowsInSection sectionIndex: Int) -> Int {
return self.arrTableData.count
}
and in cell for row at index path
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//create `potters` object with the value and use it else you can direcly use the value of objects as below.
let dictPotters = self.arrTableData[indexPath.row]
let title = dictPotters["title"]
}
Thanks

Related

How to return an array of objects using SearchBar and for loop

I am wanting the search bar to return the title for each object in the Array. I believe the problem is in my for loop but I am not completed sure. Hopefully, you guys are able to tell me what I am doing wrong.
I am searching through an API. This is the array I am attempting to search through
struct ProductResponse: Decodable {
let results: [SearchResults]
}
struct SearchResults: Decodable {
let title: String
let id:Int
}
I created a for loop to run through each object in the array and get the title and id.
func fetchProduct(productName: String) {
let urlString = "\(searchURL)&query=\(productName)&number=25"
performRequest(urlString: urlString)
}
func performRequest(urlString: String) {
// Create a URL
if let url = URL(string: urlString) {
//Create a URLSession
let session = URLSession(configuration: .default)
// Give the session a task
let task = session.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!)
return
}
if let safeData = data {
self.parseJSON(productTitle: safeData)
}
}
// Start the task
task.resume()
}
}
func parseJSON(productTitle: Data) {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(ProductResponse.self, from: productTitle)
print(decodedData.results)
} catch {
print(error)
}
}
}
I created this function to reload the data in my tableview. When I search for the product results in the Search Bar, my tableview doesn't return anything. The goal is to have my tableview return each result in a tableview cell
var listOfProducts = [SearchResults]() {
didSet {
DispatchQueue.main.async {
self.tableView.reloadData()
self.navigationItem.title = "\(self.listOfProducts.count) Products found"
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
productSearch.delegate = self
}
func downloadJSON() {
guard let downloadURL = url else { fatalError("Failed to get URL")}
URLSession.shared.dataTask(with: downloadURL) { (data, Response, error) in
print("downloaded")
}.resume()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return listOfProducts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let product = listOfProducts[indexPath.row].title
cell.textLabel?.text = product
return cell
}
}
extension ProductsTableViewController: UISearchBarDelegate {
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if let searchBarText = searchBar.text {
searchRequest.fetchProduct(productName: searchBarText)
}
}
}
This is the result
enter image description here
If everything goes well and You got the data under "decodedData.results" of "parseJSON" method, And I saw "decodedData.results" and "listOfProducts" both variables are the same type of SearchResults. So you can just add the one line of under "parseJSON" method as follows:-
func parseJSON(productTitle: Data) {
let decoder = JSONDecoder()
do {
let decodedData = try decoder.decode(ProductResponse.self, from: productTitle)
print(decodedData.results)// it have some array data of type SearchResults
self.listOfProducts = decodedData.results
self.tableView.reloadData()
} catch {
print(error)
}
}

My object array is nil while my data are correct

I try to display my data in a tableView using no framework to parse my data, but when I add my data to my table and debug it, it is nil at the output while my data I retrieve are well parses, have I forgotten something to do?
I use a structure for my parameters as this :
enum Types {
case School
case Hospital
case Station_Essence
case Restaurant
}
struct Adresse {
public var title: String
public var details: String?
public var type: Types
public var coordinate: [String: Any]
}
and in my ViewController, i proced as this :
class ListMapViewController: UIViewController {
#IBOutlet var TitleTableView: UITableView!
#IBOutlet var MapView: MKMapView!
var adresse: [Adresse]?
override func viewDidLoad() {
super.viewDidLoad()
self.TitleTableView.register(UINib(nibName: "ListMapTableViewCell", bundle: nil), forCellReuseIdentifier: "Adresse")
self.TitleTableView.delegate = self
self.TitleTableView.dataSource = self
guard let POI = URL(string: "https://moc4a-poi.herokuapp.com/") else {
return
}
let task = URLSession.shared.dataTask(with: POI) { (data, response, error) in
guard let dataResponse = data else { return }
if let json = try! JSONSerialization.jsonObject(with: dataResponse, options:[]) as? [[String: Any]] {
for data in json {
let title = data["title"] as! String
let details = data["details"] as? String
guard let type = data["type"] as? Int else { return }
let valueType = self.valueType(dataType: type)
guard let coordinates = data["coordinates"] as? [String: Any] else { return }
self.adresse?.append(Adresse(title: title, details: details, type: valueType, coordinate: coordinates))
}
}
print(self.adresse)
}
self.TitleTableView.reloadData()
task.resume()
}
private func valueType(dataType: Int) -> Types {
if(dataType == 1) {
return Types.School
} else if (dataType == 2) {
return Types.Hospital
} else if (dataType == 3) {
return Types.Station_Essence
} else {
return Types.Restaurant
}
}
}
extension ListMapViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.adresse?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Adresse", for: indexPath) as! ListMapTableViewCell
if let adresse = self.adresse?[indexPath.row] {
cell.draw(adresse: adresse)
}
return cell
}
}
extension ListMapViewController: UITableViewDelegate {
}
You have two big problems.
self.adresse is nil. You never assign it a value. So all of the self.adresse?... do nothing.
You call reloadData too soon. It needs to be done inside the completion block, after you update the data. And it needs to be on the main queue.
To fix #1, change var adresse: [Adresse]? to var adresse = [Adresse](). Then you can get rid of all the ? after uses of adresse.
To fix #2, add:
DispatchQueue.main.async {
self.TitleTableView.reloadData()
}
just after the print at the end of the completion block. Don't forget to remove the current call to reloadData.

Search Bar with JSON Objects in TableView - Swift

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UISearchDisplayDelegate{
#IBOutlet weak var recipeTable: UITableView!
#IBOutlet weak var searchbarValue: UISearchBar!
// search functionality
var filteredAnswers: [JSON]?
func searchBarSearchButtonClicked(_ searchBar: UISearchBar){
self.filteredAnswers?.removeAll()
if (searchBar.text?.isEmpty)! {
self.filteredAnswers = self.recipes } else {
if self.recipes.count > 0 {
for i in 0...self.recipes.count - 1 {
let answer = self.recipes[i] as [Dictionary<String, AnyObject>]
if answer.title.range(of: searchBar.text!, options: .caseInsensitive) != nil {
self.filteredAnswers.append(answer)
}
}
}
}
recipeTable.reloadData();
recipeTable.reloadInputViews();
searchBar.resignFirstResponder()
}
//end search parameters
// tableview functionionalitys
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return recipes.count
}
// tableview functionalities
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! RecipeTableViewCell
cell.recipeLabel.text = recipes[indexPath.row].title
//cell.textLabel?.text = recipe.title
//cell.imageView?.image = recipe.imageUrl
return cell
}
// structs for json
struct Root : Decodable {
let count : Int
let recipes : [Recipe]
}
struct Recipe : Decodable { // It's highly recommended to declare Recipe in singular form
let recipeId : String
let imageUrl, sourceUrl, f2fUrl : URL
let title : String
let publisher : String
let socialRank : Double
let page : Int?
let ingredients : [String]?
}
//recipes is array of Recipes
var recipes = [Recipe]() // array of recipes
//unfiltered recipes to put into search
var filteredRecipes = [Recipe]()
fileprivate func getRecipes() {
let jsonURL = "http://food2fork.com/api/search?key=264045e3ff7b84ee346eb20e1642d9d9"
//.data(using: .utf8)!
//let somedata = Data(jsonURL.utf8)
guard let url = URL(string: jsonURL) else{return}
URLSession.shared.dataTask(with: url) {(data, response , err) in
if let response = response as? HTTPURLResponse, response.statusCode != 200 {
print(response.statusCode)
return
}
DispatchQueue.main.async {
if let err = err{
print("failed to get data from URL",err)
return
}
guard let data = data else{return}
//print(String(data: data, encoding: .utf8))
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let result = try decoder.decode(Root.self, from: data)
self.recipes = result.recipes
//print(result.recipes)
self.recipeTable.reloadData()
}catch let jsonERR {
print("Failed to decode",jsonERR)
}
}
}.resume()
}
override func viewDidLoad() {
super.viewDidLoad()
//search functionalities
self.searchbarValue.delegate = self
//call json object
getRecipes()
}
}
I am trying to implement a search bar that takes ingredients from the JSON Object and shows the recipes that contain those ingredients in my table view. I am hoping for some best practices and help with this. I have tried a couple different strategies and none seem to be working.
This is the last one I have tried to implement, but I am getting errors in the search functionality.
self.recipes.count in searchBarSearchButtonClicked Cannot assign value
of type '[ViewController.Recipe]' to type '[JSON]?
But I'm also getting an assertion failure in -
[UISearchResultsTableView
_dequeueReusableCellWithIdentifier:forIndexPath:usingPresentationValues:]
I would like to get help but also improve and find the best way to do this. Thanks.
First of all your logic to filter the recipes cannot work and is very, very inefficient. It seems you copied and pasted the code from a completely unrelated source.
Basically the type of the data source array and the type of the filtered array must be the same, so you have to use filteredRecipes rather than filteredAnswers.
To filter the recipes with matching ingredients use filter and contains
func searchBarSearchButtonClicked(_ searchBar: UISearchBar){
filteredRecipes.removeAll()
if let searchText = searchBar.text, !searchText.isEmpty {
self.filteredRecipes = self.recipes.filter { recipe in
guard let ingredients = recipe.ingredients else { return false }
return ingredients.contains { $0.range(of: searchText, options: .caseInsensitive) != nil }
}
} else {
self.filteredRecipes = self.recipes
}
recipeTable.reloadData();
recipeTable.reloadInputViews();
searchBar.resignFirstResponder()
}
Actually this code is supposed to be executed in the delegate method
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
rather than in searchBarSearchButtonClicked
And – very important – you have to add a boolean property to indicate isSearching and in all related datasource and delegate methods you have to add a condition to show the data of filteredRecipes if isSearching is true.

How to work with Core Data saving JSON response,Show data when internet is offline in Swift 3?

I have already parsed JSON and showing in tableView which is working fine. Now my question is how will i save data offline and show when internet is not available offline using Core Data. I am working in Swift 3. If anyone can help me with screenshot it will be great help.
Below is my Code for fetching json and showing on tableView :
import UIKit
import SystemConfiguration
struct CellData {
var name:String
var address:String
public init(name:String,address:String){
self.name = name
self.address = address
}
}
///ViewController
class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
#IBOutlet weak var tableViewData: UITableView!
var arrayData = [CellData]()
override func viewDidLoad() {
super.viewDidLoad()
if Reachability.isConnectedToNetwork(){
print("Internet Connection Available!")
fetchServerData()
}else{
let alert = UIAlertController(title: "No Internet connection", message: "Please ensure you are connected to the Internet", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
print("Internet Connection not Available!")
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! MyCellData
cell.lblTop.text = "😀\(arrayData[indexPath.row].name)"
cell.lblBottom.text = arrayData[indexPath.row].address
return cell
}
func fetchServerData(){
let prs = [
"author_id": "1780",
"get_deals_author": "1" as String
]
Service.StartWithoutLoading(prs as [String : AnyObject]?, onCompletion: { result in
let json = result as? NSDictionary
if let data = json as? [String:Any]{
if let err = data["status"] as? String, err == "success"{
if let data = data["result"] as? [Any]{
var arrayData = [CellData]()
for sectionObj in data{
if let sectionObjVal = sectionObj as? [String:Any]{
if let name_deal = sectionObjVal["name"] as? String{
if let address_deal = sectionObjVal["address"] as? String{
let dataValue = CellData.init(name: name_deal, address: address_deal)
arrayData.append(dataValue)
}
}
}
}
DispatchQueue.main.async { () -> Void in
self.arrayData.removeAll()
self.arrayData = arrayData
self.tableViewData.reloadData()
}
}
}
}
})
}
}
For Core Data, you need to create the entities you need in CoreData model .xcdatamodeld. Click on Add Entity and name your entity. Then add attributes which you require to save.
You can see this link on how to create the entities and attributes. After creating everything, we can write a CoreDataStack and a manager class or we can directly use the code pre-written in AppDelegate when we check on Core Data when creating a project. I'll here use the CoreDataStack class.
Here is the class
import Foundation
import CoreData
class CoreDataStack: NSObject {
static let moduleName = "YourProject"
static let shared = CoreDataStack()
private override init() {
super.init()
_ = self.persistentContainer
}
func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: CoreDataStack.moduleName)
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
print("Coordinator URL - \(storeDescription)")
})
return container
}()
}
Now we can make a manager class to insert the data. Let's say your entity is Person and its attributes are name and address
Here is the CoreDataManager class to insert, update, fetch data.
import UIKit
import CoreData
class CoreDataManager: NSObject {
class func addRecord(object:[String:Any]) {
let person = NSEntityDescription.insertNewObject(forEntityName: "Person", into: CoreDataStack.shared.persistentContainer.viewContext) as! Person
person.name = object["name"] as? String
person.address = object["address"] as? String
CoreDataStack.shared.saveContext()
}
class func getRecords() -> [Person]? {
let request:NSFetchRequest<Person> = Person.fetchRequest()
do {
let results = try CoreDataStack.shared.persistentContainer.viewContext.fetch(request)
return results
} catch {
print(error.localizedDescription)
}
return nil
}
}
You can call addRecord method in your ViewController class and it will save your data. I recommend that you pass the complete array and then add in core data and finally call saveContext().
Finally you can use getRecords to get all records.

nil while parsing JSON in Swift

I am doing some easy projects to learn new things. I started parsing JSON with SwiftyJSON. I am trying to show some JSON data to the tableView but now I am stuck. I do not know where is the nil and why. Can you help me guys? In given code I am trying to get the "Brands" and show them inside tableView or at least print those into console.
This is the .json file I have:
{
"Snuses": {
"Brands":{
"CATCH": [
{"Products":"white", "nicotine":"8.0"},
{"Products":"yellow", "nicotine":"8.0"}
],
"GENERAL": [
{"Products":"brown", "nicotine":"8.0"},
{"Products":"white", "nicotine":"8.0"}
]
}
}
}
And here I try to get the info like this:
var numberOfRows = 0
var snusBrandsArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
parseJSON()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func parseJSON(){
let path: String = NSBundle.mainBundle().pathForResource("snuses", ofType: "json") as String!
let jsonData = NSData(contentsOfFile: path) as NSData!
let readableJSON = JSON(data: jsonData, options: NSJSONReadingOptions.MutableContainers, error: nil)
var brands = readableJSON["Snuses", "Brands"]
NSLog("\(brands)")
numberOfRows = readableJSON["Snuses"].count
for i in 1...numberOfRows{
var brands = "Snuses"
brands += "\(i)"
var name = readableJSON["Snuses", "Brands"].string as String!
snusBrandsArray.append(name)
}
}
What about something simple, like this? Below is Playground code but the parsing is the same.
//: Playground
import UIKit
import Foundation
var jsonStr = "{ \"Snuses\": { \"Brands\":{ \"CATCH\": [ {\"Products\":\"white\", \"nicotine\":\"8.0\"}, {\"Products\":\"yellow\", \"nicotine\":\"8.0\"} ], \"GENERAL\": [ {\"Products\":\"brown\", \"nicotine\":\"8.0\"}, {\"Products\":\"white\", \"nicotine\":\"8.0\"} ] } } }"
func parseJSON(jsonStr:String) throws -> [AnyObject]? {
var brandNameKeys:[AnyObject]?
let jsonData = jsonStr.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let json = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions())
if let brandNameDict = json["Snuses"]!?["Brands"] as? NSDictionary
{
brandNameKeys = brandNameDict.allKeys
}
return brandNameKeys
}
if let result = try parseJSON(jsonStr)
{
print(result)
}
In my Playground this outputs ["CATCH", "GENERAL"] which I think is what you want.
Here's a full UITableViewController demonstrating the solution in use:
import UIKit
class TableViewController: UITableViewController {
var data:[AnyObject]?
override func viewDidLoad() {
super.viewDidLoad()
if let path: String = NSBundle.mainBundle().pathForResource("Data", ofType: "json")
{
do
{
let jsonStr = try String(contentsOfFile: path)
data = try parseJSONStr(jsonStr)
}
catch _ {
print("Loading json failed")
}
}
}
// JSON Parsing
func parseJSONStr(jsonStr:String) throws -> [AnyObject]? {
var brandNameKeys:[AnyObject]?
let jsonData = jsonStr.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let json = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions())
if let brandNameDict = json["Snuses"]!?["Brands"] as? NSDictionary
{
brandNameKeys = brandNameDict.allKeys
}
return brandNameKeys
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let data = data
{
return data.count
}
else
{
return 0
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("SampleCell", forIndexPath: indexPath)
if let rowData = data![indexPath.row] as? String
{
cell.textLabel?.text = rowData
}
return cell
}
}