值存储在数组中但不可访问?

时间:2016-02-08 19:56:32

标签: ios json swift

我已经对web api进行了排序,它看起来像这样,并存储了key" attributevalue"的值。在数组中,但访问它时不显示 任何消化

{
  "responsemsg": "successfull",
  "responsecode": "200",
  "data": {
          "sku": "arv-95x103535",
           "description": "<p>watercolor on paper</p>\r\n",
           "shortdescription": "",
"prodattribute": [
  {
    "attrkeyvalue": "",
    "fk_CategoryId": 78,
    "colorimgpath": "",
    "attributevalue": "10",
    "lablename": " Height Inch",
    "fk_elementdetailid": 12,
    "IsRequired": true
  },
  {
    "attrkeyvalue": "",
    "fk_CategoryId": 78,
    "colorimgpath": "",
    "attributevalue": "9.5",
    "lablename": " Width Inch",
    "fk_elementdetailid": 13,
    "IsRequired": true
  },
  {
    "attrkeyvalue": "",
    "fk_CategoryId": 78,
    "colorimgpath": "",
    "attributevalue": "N.A",
    "lablename": " Year ",
    "fk_elementdetailid": 15,
    "IsRequired": true
  },
  {
    "attrkeyvalue": "",
    "fk_CategoryId": 78,
    "colorimgpath": "",
    "attributevalue": "Water color on paper",
    "lablename": " Medium ",
    "fk_elementdetailid": 16,
    "IsRequired": true
  },
  {
    "attrkeyvalue": "",
    "fk_CategoryId": 78,
    "colorimgpath": "",
    "attributevalue": "right bottom",
    "lablename": " Signature Position ",
    "fk_elementdetailid": 17,
    "IsRequired": true
  },
  {
    "attrkeyvalue": "",
    "fk_CategoryId": 78,
    "colorimgpath": "",
    "attributevalue": "Canvas",
    "lablename": " Surface ",
    "fk_elementdetailid": 19,
    "IsRequired": true
  }
]

这是我的解析代码

   let jsonProductAttributes = jsonResult!["data"]!["prodattribute"] as! [AnyObject]
        print("The product Arrtibutes are",jsonProductAttributes)
        var artAttributesArry = [details]()
    for attri in jsonProductAttributes {
        let artDetails = details();
        artDetails.values = attri["attributevalue"]as! String
        artAttributesArry.append(artDetails)
   print("The attributed values are ",attri["attributevalue"]as! String)

    }

这是我的模特课

 class DetailProduct {
var attributes: [details]!

}

class details {
var values: String!
}

如果任何改动或代码更改

,任何建议都会非常有用

谢谢

1 个答案:

答案 0 :(得分:0)

最后你的JSON丢失了:

 }
}

以下我使用AlamofireSwiftyJSON进行了测试,有效:

import Foundation
import Alamofire
import SwiftyJSON

class APIWrapper {

    func getJSON() {

        print("Making request");
        Alamofire.request(.GET, "http://localhost:8000/swift") .responseJSON {
            (request, response, result) -> Void in

            if let value = result.value {
                let json = JSON(value)
                self.parseJSON(json)
            }
        }

    }

    func parseJSON(json: JSON) {

        for result in json["data"]["prodattribute"].arrayValue {
            print(result["attributevalue"].stringValue)
        }
    }
}

使用以下JSON:

{
  "responsemsg": "successfull",
  "responsecode": "200",
  "data": {
          "sku": "arv-95x103535",
           "description": "<p>watercolor on paper</p>\r\n",
           "shortdescription": "",
"prodattribute": [
  {
    "attrkeyvalue": "",
    "fk_CategoryId": 78,
    "colorimgpath": "",
    "attributevalue": "10",
    "lablename": " Height Inch",
    "fk_elementdetailid": 12,
    "IsRequired": true
  },
  {
    "attrkeyvalue": "",
    "fk_CategoryId": 78,
    "colorimgpath": "",
    "attributevalue": "9.5",
    "lablename": " Width Inch",
    "fk_elementdetailid": 13,
    "IsRequired": true
  },
  {
    "attrkeyvalue": "",
    "fk_CategoryId": 78,
    "colorimgpath": "",
    "attributevalue": "N.A",
    "lablename": " Year ",
    "fk_elementdetailid": 15,
    "IsRequired": true
  },
  {
    "attrkeyvalue": "",
    "fk_CategoryId": 78,
    "colorimgpath": "",
    "attributevalue": "Water color on paper",
    "lablename": " Medium ",
    "fk_elementdetailid": 16,
    "IsRequired": true
  },
  {
    "attrkeyvalue": "",
    "fk_CategoryId": 78,
    "colorimgpath": "",
    "attributevalue": "right bottom",
    "lablename": " Signature Position ",
    "fk_elementdetailid": 17,
    "IsRequired": true
  },
  {
    "attrkeyvalue": "",
    "fk_CategoryId": 78,
    "colorimgpath": "",
    "attributevalue": "Canvas",
    "lablename": " Surface ",
    "fk_elementdetailid": 19,
    "IsRequired": true
  }
]
}
}