gpt4 book ai didi

ios - 值存储在数组中但无法访问?

转载 作者:行者123 更新时间:2023-11-30 13:43:29 26 4
gpt4 key购买 nike

我已经对如下所示的 Web api 进行了排序,并将键“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!
}

如果代码有任何更改或更改,任何建议都会非常有帮助

谢谢

最佳答案

最后您的 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
}
]
}
}

关于ios - 值存储在数组中但无法访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35278187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com