Swift 3解析JSON嵌套问题

时间:2017-04-28 06:01:37

标签: ios json swift alamofire

我正在尝试解析一些json数据,我正在努力理解如何拉出并分配嵌套值。我可以管理第一级,但是当它进一步发展时,我就会挣扎。

我有一个parseProductsData函数,它接收数据并与第一级嵌套一起正常工作。 我如何解析[“选项”:“值”],[“变体”]的嵌套Json数组。

这是我的Json。

{
  products: [
    {
      id: 8931647873,
      title: "A bug Life1123",
      body_html: "
Ban neckline
Printed front
Full sleeves
Straight hem
Printed back
100% khaddar shirt without embroidery
 
",
      vendor: "Sapphire",
      product_type: "Configurable Products",
      created_at: "2017-03-01T23:54:41+05:00",
      handle: "a-bug-life",
      updated_at: "2017-03-17T16:30:46+05:00",
      published_at: "2017-01-07T12:33:00+05:00",
      template_suffix: "",
      published_scope: "global",
      tags: "Khaddar, L, M, Women, XL, Yellow",
      variants: [...],
      options: [
        {
          id: 10702667329,
          product_id: 8931647873,
          name: "Size",
          position: 1,
          values: [
            "XXS",
            "S",
            "M",
            "L",
            "XL",
            "XXL"
          ]
        },
        {
          id: 10702667393,
          product_id: 8931647873,
          name: "Color",
          position: 2,
          values: [
            "Yellow"
          ]
        }
      ],
      images: [
        {
          id: 20808811009,
          product_id: 8931647873,
          position: 1,
          created_at: "2017-03-01T23:54:41+05:00",
          updated_at: "2017-03-01T23:54:41+05:00",
          src: "https://cdn.shopify.com/s/files/1/1814/9759/products/a_bugs_life.jpg?v=1488394481",
          variant_ids: []
        },
        {
          id: 20808811073,
          product_id: 8931647873,
          position: 2,
          created_at: "2017-03-01T23:54:41+05:00",
          updated_at: "2017-03-01T23:54:41+05:00",
          src: "https://cdn.shopify.com/s/files/1/1814/9759/products/a_bugs_life..jpg?v=1488394481",
          variant_ids: []
        },
        {
          id: 20808811137,
          product_id: 8931647873,
          position: 3,
          created_at: "2017-03-01T23:54:41+05:00",
          updated_at: "2017-03-01T23:54:41+05:00",
          src: "https://cdn.shopify.com/s/files/1/1814/9759/products/a_bugs_life.__2.jpg?v=1488394481",
          variant_ids: []
        }
      ],
      image: {...}
    },

func parseProductsData() {
    Alamofire.request(BASE_URL+"/admin/products.json").responseJSON { response in
        let result = response.result
        if let dict = result.value as? Dictionary<String, AnyObject> {
            if let list = dict["products"] as? [Dictionary<String, AnyObject>] {
                for i in 0..<list.count {
                    print(list[i])
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:8)

您所要做的就是了解层次结构和数据类型,以便您可以将其转换为正确的格式。 在您的密钥&#34;产品&#34;中,您的结构大部分类似于字典数组。你将获取一个dict数组并将其存储到变量说明列表中。您现在可以迭代列表并获取字典。然后从dict中你可以获得键上的值&#34; id &#34;,&#34; title &#34;,&#34;的供应商&#34;和类型转换进入字符串。对于&#34; 选项&#34;,&#34; 图片&#34;等密钥,您再次必须将其强制转换为数组词典,过程也是如此。只需了解数据类型并以给定格式对其进行类型转换。

func parseProductsData() {
        Alamofire.request(BASE_URL+"/admin/products.json").responseJSON { response in
            let result = response.result
            if let dict = result.value as? Dictionary<String, AnyObject> {
                if let list = dict["products"] as? [Dictionary<String, AnyObject>] {
                    for dict in 0..<list {
                        //Now if you want to fetch the value on key "Options", you can see that your list of product holds an array Of Dictionary
                        //so all you have to do is

                        let arrOFOptions = dict["options"] as? [Dictionary<String, AnyObject>]

                        //Same goes for variants

                        let arrOfVariants = dict["variants"] as? [Dictionary<String, AnyObject>]
                    }
                }
            }
        }
    }