새로운 것들을 배우기위한 몇 가지 간단한 프로젝트를 수행하고 있습니다. 으로 JSON
을 구문 분석하기 시작했습니다. tableView
에 일부 JSON
데이터를 표시하려하지만 지금은 붙어 있습니다. 나는 어디에서 왜 없는지, 왜 그런지 모릅니다. 너희들 나를 도울 수 있니? 주어진 코드에서 "Brands"
을 얻고 tableView
안에 표시하거나 적어도 console
에 인쇄하려고합니다.Swift에서 JSON을 구문 분석하는 동안 nil
이것은 내가 가지고있는 .json
파일 : 나는이 같은 정보를 얻으려고 여기
{
"Snuses": {
"Brands":{
"CATCH": [
{"Products":"white", "nicotine":"8.0"},
{"Products":"yellow", "nicotine":"8.0"}
],
"GENERAL": [
{"Products":"brown", "nicotine":"8.0"},
{"Products":"white", "nicotine":"8.0"}
]
}
}
}
을 그리고 :
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)
}
}
대부분의 인덱스는 for 루프는 0으로 시작하는 변경을 시도 해 봤나 0에서 시작 :
여기에 사용되는 솔루션을 시연 전체있는 UITableViewController이야? 나는 SwiftyJSON을 시도하지 않았다. – kometen
@kometen 그래, 나는 그것을 시도했다. –
정확히 무엇을 원하니? '브랜드'는 '잡다'와'일반'이라는 두 가지 속성을 가지고 있는데'사전'의 배열이다. '{ "Products": "white", "nicotine": "8.0"}'또는 브랜드 이름을'catch '및'general'와 같은 사전 값으로 원하십니까? – triandicAnt