JSON:使用 Alamofire 和 Swift 3 提取数据
Posted
技术标签:
【中文标题】JSON:使用 Alamofire 和 Swift 3 提取数据【英文标题】:JSON: Extracting Data Using Alamofire and Swift 3 【发布时间】:2017-10-19 23:10:02 【问题描述】:谁能告诉我如何从这个 JSON 中获取数据?
这是我的代码,后面是 JSON
let URL = "URL"
Alamofire.request(URL).responseJSON
response in
print("HereBegins: = \(response)")
let result = response.result.value as? [String:Any]
返回的 json 是...
从这里开始:= 成功: 项目 = (
addToCartUrl = "等等等等 URL";
affiliateAddToCartUrl = "废话另一个 URL";
在线可用 = 0;
品牌名称 = 通用;
捆绑 = 0;
categoryNode = "976759_1071964_976788";
categoryPath = "食品/新鲜食品/乳制品、鸡蛋和奶酪/牛奶和奶油";
间隙 = 0;
customerRating = "5.0";
customerRatingImage = "http://i2.walmartimages.com/i/CustRating/5.gif";
freeShipToStore = 0;
freeShippingOver50Dollars = 1;
giftOptions =
allowGiftMessage = 0;
allowGiftReceipt = 0;
allowGiftWrap = 0;
;
图像实体 =
(
entityType = SECONDARY;
largeImage = "https://i5.walmartimages.com/asr/c2bc3886-fb4f-4c46-b7e9-0cc4ebabc7c1_1.d42acf75b29b132532d5953b139b115f.jpeg?odnHeight=450&odnWidth=450&odnBg=FFFFFF";
mediumImage = "https://i5.walmartimages.com/asr/c2bc3886-fb4f-4c46-b7e9-0cc4ebabc7c1_1.d42acf75b29b132532d5953b139b115f.jpeg?odnHeight=180&odnWidth=180&odnBg=FFFFFF";
thumbnailImage = "https://i5.walmartimages.com/asr/c2bc3886-fb4f-4c46-b7e9-0cc4ebabc7c1_1.d42acf75b29b132532d5953b139b115f.jpeg?odnHeight=100&odnWidth=100&odnBg=FFFFFF";
,
entityType = PRIMARY;
largeImage = "https://i5.walmartimages.com/asr/1d575fe6-b8c9-4dc6-821b-42cbde621373_1.4764431b7ebfb4707c0a2f7fe47e85cc.jpeg?odnHeight=450&odnWidth=450&odnBg=FFFFFF";
mediumImage = "https://i5.walmartimages.com/asr/1d575fe6-b8c9-4dc6-821b-42cbde621373_1.4764431b7ebfb4707c0a2f7fe47e85cc.jpeg?odnHeight=180&odnWidth=180&odnBg=FFFFFF";
thumbnailImage = "https://i5.walmartimages.com/asr/1d575fe6-b8c9-4dc6-821b-42cbde621373_1.4764431b7ebfb4707c0a2f7fe47e85cc.jpeg?odnHeight=100&odnWidth=100&odnBg=FFFFFF";
);
isTwoDayShippingEligible = 0;
itemId = 46491746;
largeImage = "https://i5.walmartimages.com/asr/1d575fe6-b8c9-4dc6-821b-42cbde621373_1.4764431b7ebfb4707c0a2f7fe47e85cc.jpeg?odnHeight=450&odnWidth=450&odnBg=FFFFFF";
longDescription = "超值香草杏仁奶,64 液量盎司:
味道鲜美 超值杏仁奶上市一个 64 液量盎司的纸盒 添加了维生素 A、D 和 E 添加了钙 每份仅 80 卡路里 ”;市场 = 0;
mediumImage = "https://i5.walmartimages.com/asr/1d575fe6-b8c9-4dc6-821b-42cbde621373_1.4764431b7ebfb4707c0a2f7fe47e85cc.jpeg?odnHeight=180&odnWidth=180&odnBg=FFFFFF";
型号 = 11951;
name = "超值香草杏仁奶,64 液量盎司";
ninetySevenCentShipping = 0;
评论数 = 3;
offerType = "STORE_ONLY";
parentItemId = 46491746;
preOrder = 0;
productTrackingUrl = "http://linksynergy.walmart.com/fs-bin/click?id=|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid=1082&RD_PARM1=https%253A%252F%252Fwww.walmart.com%252Fip%252FGreat-Value-Vanilla-Almond-Milk-64-fl-oz%252F46491746%253Faffp1%253DHzNQqMKL6fuAwFZnu_RUqysY3U3RzuxH0bQKAHNYr0Q%2526affilsrc%253Dapi";
productUrl = "http://c.affil.walmart.com/t/api03?l=https%3A%2F%2Fwww.walmart.com%2Fip%2FGreat-Value-Vanilla-Almond-Milk-64-fl-oz%2F46491746%3Faffp1%3DHzNQqMKL6fuAwFZnu_RUqysY3U3RzuxH0bQKAHNYr0Q%26affilsrc%3Dapi%26veh%3Daff%26wmlspartner%3Dreadonlyapi";
salePrice = "2.58";
shipToStore = 0;
standardShipRate = 0;
库存=“不可用”;
thumbnailImage = "https://i5.walmartimages.com/asr/1d575fe6-b8c9-4dc6-821b-42cbde621373_1.4764431b7ebfb4707c0a2f7fe47e85cc.jpeg?odnHeight=100&odnWidth=100&odnBg=FFFFFF";
upc = 078742052366;
);
【问题讨论】:
为什么这被否决了?最好解释一下显而易见的事情并说......“我到处寻找,其他人的解决方案对我不起作用!请有人帮忙。”出于某种原因,我无法从此 JSON 访问任何内容。当我打印“结果”时,我可以得到一些东西,但我什至做不到结果![“项目”]但我可以走得更远。 我没有投反对票,但 JSON(顺便说一下不是 JSON)是一团糟。我们鼓励您正确格式化您的代码。 感谢您的信息。我从沃尔玛得到了 JSON 或其他任何东西,哈哈,他们称它为 JSON,所以我不知道它是什么。 【参考方案1】:想通了。
if let resData = result["items"].arrayObject
arrRes = resData as! [[String:AnyObject]]
for element in arrRes[0]
print(element)
print(arrRes[0]["name"])
【讨论】:
以上是关于JSON:使用 Alamofire 和 Swift 3 提取数据的主要内容,如果未能解决你的问题,请参考以下文章
通过使用 Alamofire 和解码获取 JSON - Swift 4
Swift 使用 Alamofire 和 SwiftyJSON 解析 Json [关闭]
如何使用 Alamofire 和 Swift Decode 从 JSON 解码和访问 Double