简体   繁体   中英

How to parse multidimension array inside json using SWIFT

this is my sample JSON data

{
    "COD": [ {    
        "Merchant": {       
            "MerchantID": "4701",
            "MerchantCompany": "PAMB",
            "MerchantName": "Phira",
            "MerchantSurname": "Mueadnok",
            "MerchantAddress": "54 MT Mansion",
            "MerchantCity": "Huaykwang",
            "MerchantProvince": "Bangkok",
            "MerchantTel": "0816547412"
        },
        "Order": 
        {    
            "OrderID": "875321",
            "ProductName": "SKII miracle water",
            "ProductPrice": "1020",
            "ProductDimention": "10X10X20 [W*H*D]",
            "ProductWeight": "200G",
            "CollectITEM": "0",
            "Deliver": "0"
        },
        "Customer": 
        {
            "CustomerName": "Malee",
            "CustomerSurname": "",
            "CustomerAddress": "54 MT Mansion",
            "CustomerCity": "Huaykwang",
            "CustomerProvince": "Bangkok",
            "CustomerTel": "0816547412"
        }    
    }]
}     

In XCode6.3

    var endpoint = NSURL(string: /* Link From site */)
    var data = NSData(contentsOfURL: endpoint!)        

    if let json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers, error: nil) as? NSDictionary {
        if let items = json["COD"] as? NSArray {
            for item in items{
               println(item["Order"])
                println("_______________________________________")
                if let order = item["Order"] as? NSDictionary {
                    for orderDetail in order {

                        println(orderDetail["OrderID"])
                        // i need to present only OrderID
                    }
                }
            }
        }
    }

I use this code is parse JSON array but the out put just coming out only the list in "Order" I need to use only " OrderID ". can anybody help me to fix it out

Try the SwiftyJSON library

https://github.com/SwiftyJSON/SwiftyJSON

It simplifies everything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM