Reputation: 1246
I can't get my array of string from my JSON using SwiftyJSON. I have json like this
{
"categories": {
"202": "Women's Clothing",
"104": "Men's Clothing"
},
"products": [{
"price": 528500.0,
"title": "Some title with long name",
"id": "13864671"
}..., ..., ...],
"keywords": ["hoodie", "hoodie paramore i", "hoodie daft punk", "hoodie muse", "hoodie nirvana"]
}
My problem is, I can print categories and products but I cant print keywords. It just give me blank array []
. So what's wrong here?
Alamofire.request(request).responseJSON {
response in
if response.result.error == nil {
let json = JSON(response.result.value!)
print(json)
success(json)
}else{
error("\(response.result.error?.localizedDescription)")
}
}
When I print json
, my categories
and products
are fine except keywords
that give me no value []
.
Here is my log on Xcode
{
"categories" : {
"111" : "Men's Clothing",
"122" : "Women's Clothing"
},
"keywords" : [
],
"products" : [
{
"price" : 123,
"title" : "Long name product",
"id" : "123123"
}
]
}
Any help would be appreciated. Thank you!
Upvotes: 0
Views: 419
Reputation: 25304
Try this code:
Product.swift
import Foundation
import SwiftyJSON
class Product {
var price: Double?
var title: String?
var id: String?
init (json: JSON) {
if let price = json["price"].double {
self.price = price
}
if let title = json["title"].string {
self.title = title
}
if let id = json["id"].string {
self.id = id
}
}
var description: String {
get {
var _description = ""
if let price = self.price {
_description += "price: \(price)\n"
}
if let title = self.title {
_description += "title: \(title)\n"
}
if let id = self.id {
_description += "id: \(id)\n"
}
return _description
}
}
}
ViewController.swift
import UIKit
import SwiftyJSON
class ViewController: UIViewController {
var keywords = [String]()
var categories = [String:String]()
var products = [Product]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if let path = NSBundle.mainBundle().pathForResource("data", ofType: "json") {
if let data = NSData(contentsOfFile: path) {
let json = JSON(data: data)
//NSLog("\(json)")
if let keywords = json["keywords"].array {
for keyword in keywords {
if let keyword = keyword.string {
self.keywords.append(keyword)
}
}
}
if let categories = json["categories"].dictionary {
for key in categories.keys {
if let category = categories[key]?.stringValue {
self.categories.updateValue(category, forKey: key)
}
}
}
if let products = json["products"].array {
for product in products {
self.products.append(Product(json: product))
}
}
}
}
print("keywords: \(keywords)")
print("categories: \(categories)")
print("products:\n")
for product in products {
print(product.description)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
data.json
{
"categories": {
"202": "Women's Clothing",
"104": "Men's Clothing"
},
"products": [{
"price": 528500.0,
"title": "Some title with long name",
"id": "13864671"
},
{
"price": 528531200.0,
"title": "!!Some title with long name",
"id": "13223864671"
}],
"keywords": ["hoodie", "hoodie paramore i", "hoodie daft punk", "hoodie muse", "hoodie nirvana"]
}
result
Upvotes: 1