PRADIP KUMAR PRADIP KUMAR - 5 months ago 26
JSON Question

How to parse the json data using swiftJson along with Alamofire

my Json data sample. this is only a sample of data .Likewise so much data available like number of subcategory is 22. number of items are different according to subcategory.Also number of rows are 15 in which the first name is Pizza.

[
{
"id": "244",
"name": "PIZZAS",
"image": "",
"coupon": "1",
"icon": "",
"order": "1",
"aname": "",
"options": "2",
"subcategory": [
{
"id": "515",
"name": "MARGARITA",
"description": "Cheese and Tomato",
"image": "",
"icon": "",
"coupon": "1",
"order": "1",
"aname": "",
"options": "2",
"item": [
{
"id": "1749",
"name": "9 Inch Thin & Crispy Margarita",
"description": "",
"price": "3.40",
"coupon": "1",
"image": "",
"options": "2",
"order": "1",
"addon": "495",
"aname": "",
"icon": ""
}]
}]
}]


enter image description here enter image description here

I want to fetch the all name, all subcategory name, all item name and all item description into particular array so that i can populate particularly into different tableview

func ParseJSON(){
var MenuList = [Menu]()

Alamofire.request(.GET, myUrl)
.validate()
.responseJSON { response in
switch response.result
{
case .Success:
if let value = response.result.value {
let json = JSON(value)
print("JSON: \(json)")
for entity in json{
print(entity)
MenuList.append(entity["name"]) ///error
}

}
case .Failure(let error):
print(error)
}
}

}


I am able to fetch the response using Alamofire. Now How to implement it using swiftJson ??

Updated

I have added the class model for main category, subcategory and items.

import UIKit
class Menu {
var id: Int?
var name: String?
var image: UIImage?
var coupon: Int?
var icon: UIImage?
var order: Int?
var aname: Int?
var options: Int?

init(id:Int,name:String,image:UIImage,coupon:Int,icon:UIImage,order:Int,aname:Int,options:Int){
self.id = id
self.name = name
self.image = image
self.coupon = coupon
self.icon = icon
self.order = order
self.aname = aname
self.options = options


}
}


how to pass the data to the class and get the value

Answer

Your json is already a SwiftyJSON object.

These objects always have an index key and a content key.

So to loop over the json you need for (_, entity) in json instead of for entity in json.

The _ part is the index that we ignore. We could also do for (index, entity) in json if we wanted to use the index.

You also need to use SwiftyJSON's type properties, like .string (optional) or .stringValue (non optional).

In your model class, the properties types have to reflect the ones you get.

To populate an array of Menu objects from the first level of your JSON, you could adapt your model like this:

class Menu {
    var id: Int?
    var name: String?
    var image: String?
    var coupon: Int?
    var icon: String?
    var order: Int?
    var aname: String?
    var options: Int?

    init(id: Int?, name: String?, image: String?, coupon: Int?, icon: String?, order: Int?, aname: String?, options: Int?) {
        self.id = id
        self.name = name
        self.image = image
        self.coupon = coupon
        self.icon = icon
        self.order = order
        self.aname = aname
        self.options = options
    }
}

Then populate an array from the JSON:

var menus = [Menu]()

for (_, content) in json {
    let menu = Menu(id: Int(content["id"].stringValue),
                    name: content["name"].string,
                    image: content["image"].string,
                    coupon: content["coupon"].int,
                    icon: content["icon"].string,
                    order: Int(content["order"].stringValue),
                    aname: content["name"].string,
                    options: Int(content["options"].stringValue))
    menus.append(menu)
}

Now you can iterate over your objects as you need:

for menu in menus {
    print(menu.name)
    print(menu.id)
}

Prints:

Optional("PIZZAS")
Optional(244)

Now if you want to also use the data in the "subcategory" of each object, you have to make other model classes which reflect these properties, like a "SubCategory" class and an "Item" class (those could be also stored in each Menu, for example). And you use the same system as my example to populate them - you just have to adapt to each format, but now that you have this working example it should be rather simple to get there. The trick is to understand your JSON structure well enough so that you can reflect it in your model objects. Focus primarily on this and the rest will follow. :)

Comments