Dhruvil Patel Dhruvil Patel - 1 month ago 6
JSON Question

need only key(Status) and value(0) from json in swift 3

{
"items": [
{
"startTime": "1498667581661",
"endTime": "1498667821661",
"dateTime": "2017-06-28T16:33:01.661Z",
"totalTime": "4",
"auctionName": "Bbbb",
"status": 1,
"id": "4760417733705728",
"kind": "auctionTimeApi#resourcesItem"
},
{
"startTime": "1498772812087",
"endTime": "1498772992087",
"dateTime": "2017-06-29T21:46:52.087Z",
"totalTime": "3",
"auctionName": "sdasdasdd",
"status": 1,
"id": "5080491044634624",
"kind": "auctionTimeApi#resourcesItem"
},
{
"startTime": "1498833895423",
"endTime": "1498834375423",
"dateTime": "2017-06-30T14:44:55.423Z",
"totalTime": "8",
"auctionName": "Boston",
"status": 1,
"id": "5085211482128384",
"kind": "auctionTimeApi#resourcesItem"
},
{
"startTime": "1498767894987",
"endTime": "1498768254987",
"dateTime": "2017-06-29T20:24:54.987Z",
"totalTime": "6",
"auctionName": "Dfddd",
"status": 0,
"id": "5111065843073024",
"kind": "auctionTimeApi#resourcesItem"
},
{
"startTime": "1498640043323",
"endTime": "1498640283323",
"dateTime": "2017-06-28T08:54:03.323Z",
"totalTime": "4",
"auctionName": "Andsda",
"status": 1,
"id": "5118511437316096",
"kind": "auctionTimeApi#resourcesItem"
},
{
"startTime": "1498807228606",
"endTime": "1498807348606",
"dateTime": "2017-06-30T07:20:28.606Z",
"totalTime": "2",
"auctionName": "Dxf",
"status": 1,
"id": "5146118144917504",
"kind": "auctionTimeApi#resourcesItem"
},
{
"startTime": "1498806518484",
"endTime": "1498807358484",
"dateTime": "2017-06-30T07:08:38.484Z",
"totalTime": "14",
"auctionName": "rrrtttt",
"status": 1,
"id": "5151952589553664",
"kind": "auctionTimeApi#resourcesItem"
},
{
"startTime": "1498807683483",
"endTime": "1498807863483",
"dateTime": "2017-06-30T07:28:03.483Z",
"totalTime": "3",
"auctionName": "wwew",
"status": 1,
"id": "5956451503702016",
"kind": "auctionTimeApi#resourcesItem"
},
{
"startTime": "1498803576630",
"endTime": "1498803816630",
"dateTime": "2017-06-30T06:19:36.630Z",
"totalTime": "4",
"auctionName": "zzzz",
"status": 0,
"id": "5964732200648704",
"kind": "auctionTimeApi#resourcesItem"
},
{
"startTime": "1498833083854",
"endTime": "1498833563854",
"dateTime": "2017-06-30T14:31:23.854Z",
"totalTime": "8",
"auctionName": "Dartmouth",
"status": 0,
"id": "6314781967384576",
"kind": "auctionTimeApi#resourcesItem"
}
],
"kind": "auctionTimeApi#resources",
"etag": "\"l-71RhD3VMYkQ-s_W643oBlpkCw/1SZlmWzcSB8XxEnhJpjVvwPV5k4\""
}


Above is my JSON data in the Google cloud 
Add This is my SWIFT3 code:

import UIKit

class ViewController: UIViewController , UITableViewDataSource {



@IBOutlet weak var auctionLabel: UILabel!

@IBOutlet weak var auctionTableView: UITableView!


var fetchAuctionName = [AuctionName]()

override func viewDidLoad() {
super.viewDidLoad()

auctionTableView.dataSource = self

parseData()
}

override var prefersStatusBarHidden: Bool {
return true
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return fetchAuctionName.count

}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = auctionTableView.dequeueReusableCell(withIdentifier: "cell")

cell?.textLabel?.text = fetchAuctionName[indexPath.row].auctionname
cell?.detailTextLabel?.text = fetchAuctionName[indexPath.row].auctionname

return cell!
}



func parseData() {

fetchAuctionName = []

let url = "https://coral-core-175.appspot.com/_ah/api/auctionTimeApi/v1/auctiontimecollection"
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = "GET"

let configuration = URLSessionConfiguration.default
let session = URLSession(configuration: configuration, delegate: nil, delegateQueue: OperationQueue.main)

let task = session.dataTask(with: request) { (data, response, error) in

if (error != nil){
print("Error")
}
else{


do{
let fetchData = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! NSDictionary

let jsonArray = fetchData.value(forKey: "items") as! NSArray


for eachFetchedAuctionName in jsonArray {

let eachAuctionName = eachFetchedAuctionName as! [String : Any]

let auctionname = eachAuctionName["auctionName"] as! String

self.fetchAuctionName.append(AuctionName(auctionname: auctionname));

}



self.auctionTableView.reloadData()



}
catch{
print("Error 2")
}
}

}
task.resume()

}

}


class AuctionName: NSObject {

var auctionname : String

init(auctionname : String) {
self.auctionname = auctionname

}
}


With this code I can print all the status values from the JSON data

My question is; I just need those with a status value of 0 to be printed. How can I modify my code to achieve this?

Answer Source
        for eachFetchedAuctionName in jsonArray {

         let eachAuctionName = eachFetchedAuctionName as! [String : Any]
         let auctionname = eachAuctionName["auctionName"] as! String
        self.fetchAuctionName.append(AuctionName(auctionname: auctionname));

    // you can typecast to either String or an Int and then compare it respectively 
   //with a String "1" or an Int(1), whichever suits your app


        guard let status = eachAuctionName["status"] as! Int else { return }
            if status == 0 {
                print("0 found")
              }
        }

One suggestion: use swift 4 Decodable protocol, to parse JSON easily into custom objects either a struct or a class of your choice, makes the code looks nice and clean.