如何定义数组数组

时间:2016-06-16 15:54:10

标签: arrays json swift

我正在尝试从Api获取Json数据,其格式如下

     [
{
"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": ""
      },
      {
        "id": "1750",
        "name": "12 Inch Thin & Crispy Margarita",
        "description": "",
        "price": "5.20",
        "coupon": "1",
        "image": "",
        "options": "2",
        "order": "2",
        "addon": "496",
        "aname": "",
        "icon": ""
      }
      ]
  }

如何获取“子类别名称”以及“项目名称”。请帮帮我。我已经写了一些代码并尝试取出但没有工作。

      var json: NSArray!
        do {
            json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions()) as? NSArray

        } catch {
            print(error)
        }

        self.AllData = json.valueForKey("subcategory") as! Array<String>
        print(self.AllData)
        print(self.AllData.count)

但它没有取得任何价值 其他方式我也试过,但仍然没有数据提取。只有数据来自json1。

    do {
            let json1 = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())
           // print(json1)

            if let subcategory = json1["subcategory"] as? [[String: AnyObject]] {
                for subname in subcategory {
                    if let name = subname["name"] as? String {
                        print(name)
                    }
                    if let items = subname["item"] as? [[String: AnyObject]] {
                        for item in items {
                            if let itemName = item["name"] as? String {
                                print(itemName)
                            }
                        }
                    }
                }
            }
        } catch {
            print(error)
        }

2 个答案:

答案 0 :(得分:0)

在剪切和粘贴问题时,这可能只是一个问题,但看起来您的JSON数据格式不正确([]和{}不匹配)。正如之前的评论员所说,你正在处理一系列字典而不是字符串数组。尝试这样的事情:

do {
    let json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())

    if let subs = json[0]["subcategory"] as? [[String: AnyObject]] {
        for sub in subs {
            if let name = sub["name"] as? String {
                print(name)
            }
            if let items = sub["item"] as? [[String: AnyObject]] {
                for item in items {
                    if let itemName = item["name"] as? String {
                        print(itemName)
                    }
                }
            }
        }
    }
} catch {
    print(error)
}

答案 1 :(得分:0)

首先是一个对象数组/ Dictionary.And你正在创建一个字符串数组。这就是为什么子类别不起作用。你需要创建一个像这样的字典数组:

        do {
            let json1 = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())

            self.AllData = json1.valueForKey("name") as! Array<String>
            print(self.AllData)


            print("Number of menu = \(json1.count)")
            for var i in 0..<json1.count {
              print(" \n \(i+1) row menu \n")


                if let subs = json1[i]["subcategory"] as? [[String: AnyObject]] {
                    //print(subs)


                    for sub in subs {

                        if let name = sub["name"] as? String {
                            print("subcategory name= \t \(name)")
                            //print(name)
                        }



                        if let desc = sub["description"] as? String {
                            print("description= \t \(desc)")
                           // print(desc)
                        }


                }

                    print("Number of subcategory= \(subs.count)")
                    for var i in 0..<subs.count {


                     if let items = subs[i]["item"] as? [[String: AnyObject]] {
                     print("items = ")
                        print(items.count)
                       for item in items {
                            if let itemName = item["name"] as? String {
                                print(itemName)

                              }
                         }
                     }
                 }


                }

            }
                        }catch {
            print(error)
        }

尝试这个,它将根据你的json数据

工作