Starter Coding Starter Coding - 3 months ago 17
JSON Question

How to loop data in JSON When string in label equal data in JSON array

I need to show data from JSON on label. But my code get error.please help me see on function

barcodeReaded
. How to loop data when String in label equal string in "testCode" on array JSON file.

This JSON file

{
"episode": [

{
"testCode": "11111111",
"title": "Stomachic mixture 180 ml",
"drug": "AAAAA",
"image": "urlImage",
"price": "100"
},
{
"testCode": "22222222",
"title": "Parasetamol 200 ml",
"drug": "BBBBB",
"image": "urlImage",
"price": "150"
},
{
"testCode": "33333333",
"title": "Beramol 300 ml",
"drug": "CCCCC",
"image": "urlImage",
"price": "120"
}

]

}


This some code

import UIKit

class barcodeViewController: UIViewController, BarcodeDelegate {


@IBOutlet weak var thumbnailImageView: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var drugLabel: UILabel!
@IBOutlet weak var priceLabel: UILabel!
@IBOutlet weak var showCodeLabel: UILabel!


var episode: Episode!

override func viewDidLoad() {
super.viewDidLoad()


}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
print("Segue!")

let barcodeViewController: barcodeCapViewController = segue.destinationViewController as! barcodeCapViewController
barcodeViewController.delegate = self

}

@IBAction func doneButtonPressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}


func barcodeReaded(barcode: String) {
print("Barcode is: \(barcode)")
showCodeLabel.text = barcode
barcode = episode.testCode

if((episode.testCode) != nil)
{
titleLabel.text = episode.title
drugLabel.text = episode.drug
priceLabel.text = episode.price
}


}

}








import Foundation

class Episode
{
var title: String?
var thumbnailURL: NSURL?
var drug: String?
var price: String?
var testCode: String?

init(title: String, thumbnailURL: NSURL, drug: String, price: String, testCode: String)
{
self.title = title
self.thumbnailURL = thumbnailURL
self.drug = drug
self.price = price
self.testCode = testCode
}

typealias EpisodeDictionary = [String : AnyObject]

init(espDictionary: EpisodeDictionary)
{
self.title = espDictionary["title"] as? String
self.thumbnailURL = NSURL(string: espDictionary["thumbnailURL"] as! String)
self.drug = espDictionary["drug"] as? String
self.price = espDictionary["price"] as? String
self.testCode = espDictionary["testCode"] as? String
}

static func downloadAllEpisodes() -> [Episode]
{
var episodes = [Episode]()

let jsonFile = NSBundle.mainBundle().pathForResource("testJson3edit6", ofType: "json")
let jsonData = NSData(contentsOfFile: jsonFile!)
if let jsonDictionary = NetworkService.parseJSONFromData(jsonData) {
let espDictionaries = jsonDictionary["episodes"] as! [EpisodeDictionary]
for dict in espDictionaries {
let episode = Episode(espDictionary: dict)
episodes.append(episode)
}
}

return episodes
}
}


This interface image

Answer

You can get detail of your barcode like this from your array of dictionary.

func barcodeReaded(barcode: String) {
    print("Barcode is: \(barcode)")
    showCodeLabel.text = barcode
    let episodes = Episode.downloadAllEpisodes()
    var filteredEpisodes = episodes.filter({ $0.testCode == barcode })
    if filteredEpisodes.count > 0 {
        titleLabel.text = filteredEpisodes[0].title
        drugLabel.text = filteredEpisodes[0].drug
        priceLabel.text = filteredEpisodes[0].price
    }   
}