iOS Playground中的Json解析do方法不解析
Posted
技术标签:
【中文标题】iOS Playground中的Json解析do方法不解析【英文标题】:Json parsing in iOS playground do method is not parsing 【发布时间】:2016-05-11 00:44:42 【问题描述】:我正在尝试根据 json 中的标签将一些 json 数据解析为三个不同的数组。我似乎被卡住了,不知道为什么我的 for 循环永远不会被输入。我是 ios 新手,正在使用它来快速学习。任何帮助将不胜感激。 这是我正在使用的代码:
var myPicture = [String]()
var myPath = [String]()
var mylabel = [String]()
let jsonString = "[\"picture\" : \"Picture 1 \", \"path\": \"Path 1\" , \"label\" : \"Label 1\"]"
//Convert jsonString to NSData
let myData = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
do
let promoJson = try NSJSONSerialization.JSONObjectWithData(myData, options:.AllowFragments)
if let promtions = promoJson[""] as? [[String: AnyObject]]
for promtions in promtions
if let picture = promtions["picture"] as? String
myPicture.append(picture)
if let path = promtions["path"] as? String
myPath.append(path)
if let label = promtions["label"] as? String
mylabel.append(label)
catch
print("Error with Json: \(error)")
print(myPicture.first)
print(myPath.first)
print(mylabel.first)
打印的结果都是零。所以没有任何东西被附加到数组中
【问题讨论】:
我不知道 promtions = promoJson[""]... 行打算做什么。即使它确实成功了,那么“促销中的促销”是绝对错误的。 【参考方案1】:if let promtions = promoJson[""]
部分不起作用,而且无论如何都没用。这只是promoJson
,您必须将其转换为字典数组。
你离解决方案并不远,看看我的代码的工作版本:
do
let promoJson = try NSJSONSerialization.JSONObjectWithData(myData, options: [])
if let promtions = promoJson as? [[String: AnyObject]]
for promtion in promtions
if let picture = promtion["picture"] as? String
myPicture.append(picture)
if let path = promtion["path"] as? String
myPath.append(path)
if let label = promtion["label"] as? String
mylabel.append(label)
catch let error as NSError
print(error.debugDescription)
另类
既然问题已经解决,让我建议您另一种方法:不要为您的数据使用单独的数组,而是使用一个对象数组来保存您的数据。
例如,制作一个这样的结构:
struct Promotion
let picture: String
let path: String
let label: String
还有一个用于该结构实例的数组:
var myPromotions = [Promotion]()
现在我们可以解码 JSON,从中创建对象,然后将它们存储在数组中:
do
let promoJson = try NSJSONSerialization.JSONObjectWithData(myData, options: [])
if let promtions = promoJson as? [[String: AnyObject]]
for promtion in promtions
if let picture = promtion["picture"] as? String,
path = promtion["path"] as? String,
label = promtion["label"] as? String
let promo = Promotion(picture: picture, path: path, label: label)
myPromotions.append(promo)
catch let error as NSError
print(error.debugDescription)
现在看数组的内容,很方便:
for promo in myPromotions
print(promo.label)
print(promo.path)
print(promo.picture)
【讨论】:
【参考方案2】:当您转换它时,它已经是一个数组。
import Foundation
import UIKit
var myPicture = [String]()
var myPath = [String]()
var mylabel = [String]()
let jsonString = "[\"picture\" : \"Picture 1 \", \"path\": \"Path 1\" , \"label\" : \"Label 1\"]"
//Convert jsonString to NSData
let myData = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
do
let promoJson = try NSJSONSerialization.JSONObjectWithData(myData, options:.AllowFragments) as! NSArray
for promtions in promoJson
if let picture = promtions["picture"] as? String
myPicture.append(picture)
if let path = promtions["path"] as? String
myPath.append(path)
if let label = promtions["label"] as? String
mylabel.append(label)
catch
print("Error with Json: \(error)")
print(myPicture.first) // "Optional("Picture 1 ")\n"
print(myPath.first) // "Optional("Path 1")\n"
print(mylabel.first) // "Optional("Label 1")\n"
这样就可以了。
【讨论】:
以上是关于iOS Playground中的Json解析do方法不解析的主要内容,如果未能解决你的问题,请参考以下文章