正在解析 JSON 数据
Parsing JSON Data
我想解析这个 JSON :http://jsonplaceholder.typicode.com/users
我在查找 JSON 结构时遇到问题。
我正在尝试 JSON 使用这种运行良好的结构,但我不确定这是更好的方法还是不是!
将此 JSON 数组解析为 post 实例的最佳方法是什么?
这是我的代码:
func profileFromJSONData(data : NSData) -> ProfileResult {
do{
let jsonObject : NSArray!
= try NSJSONSerialization.JSONObjectWithData(data, options: []) as! NSArray
for profileJSON in jsonObject {
if let profile = profileFromJsonObject(profileJSON as! NSDictionary) {
finalProfile.append(profile)
}
}
return .Success(finalProfile)
}
catch let error {
return .Failure(error)
}
}
func profileFromJsonObject(json: NSDictionary) -> UserProfile?{
guard let
id = json["id"] as? Int,
name = json["name"] as? String,
userName = json["username"] as? String,
email = json["email"] as? String,
address = json["address"] as? NSDictionary,
phone = json["phone"] as? String,
website = json["website"] as? String,
company = json["company"] as? NSDictionary
else {
return nil
}
let obj = UserProfile(id: id, name: name, userName: userName, email: email, address: address, phone: phone, website: website, company: company)
return obj
}
这是 Working with JSON in Swift,
时苹果的建议
并且您可以使用 flatMap
将代码改进为一行
更改自:
for profileJSON in jsonObject {
if let profile = profileFromJsonObject(profileJSON) {
finalProfile.append(profile)
}
}
至:
finalProfile += jsonObject.flatMap(profileFromJsonObject)
也一样
处理地址:
struct Address {
var street: String
var city: String
...
init?(json: [String: AnyObject]){...}
}
extension Address: CustomStringConvertible {
var description: String {
return "street: \(street), city: \(city)"
}
}
func profileFromJsonObject(json: [String: AnyObject]) -> UserProfile? {
guard let
...
addressJson = json["address"] as? [String: AnyObject]
address = Address(json: addressJson),
...
else {
return nil
}
let obj = UserProfile(id: id, name: name, userName: userName, email: email, address: address, phone: phone, website: website, company: company)
print(address) // output: "street: Kulas Light, city: Gwenborough"
}
我想解析这个 JSON :http://jsonplaceholder.typicode.com/users 我在查找 JSON 结构时遇到问题。 我正在尝试 JSON 使用这种运行良好的结构,但我不确定这是更好的方法还是不是! 将此 JSON 数组解析为 post 实例的最佳方法是什么? 这是我的代码:
func profileFromJSONData(data : NSData) -> ProfileResult {
do{
let jsonObject : NSArray!
= try NSJSONSerialization.JSONObjectWithData(data, options: []) as! NSArray
for profileJSON in jsonObject {
if let profile = profileFromJsonObject(profileJSON as! NSDictionary) {
finalProfile.append(profile)
}
}
return .Success(finalProfile)
}
catch let error {
return .Failure(error)
}
}
func profileFromJsonObject(json: NSDictionary) -> UserProfile?{
guard let
id = json["id"] as? Int,
name = json["name"] as? String,
userName = json["username"] as? String,
email = json["email"] as? String,
address = json["address"] as? NSDictionary,
phone = json["phone"] as? String,
website = json["website"] as? String,
company = json["company"] as? NSDictionary
else {
return nil
}
let obj = UserProfile(id: id, name: name, userName: userName, email: email, address: address, phone: phone, website: website, company: company)
return obj
}
这是 Working with JSON in Swift,
时苹果的建议并且您可以使用 flatMap
更改自:
for profileJSON in jsonObject {
if let profile = profileFromJsonObject(profileJSON) {
finalProfile.append(profile)
}
}
至:
finalProfile += jsonObject.flatMap(profileFromJsonObject)
也一样
处理地址:
struct Address {
var street: String
var city: String
...
init?(json: [String: AnyObject]){...}
}
extension Address: CustomStringConvertible {
var description: String {
return "street: \(street), city: \(city)"
}
}
func profileFromJsonObject(json: [String: AnyObject]) -> UserProfile? {
guard let
...
addressJson = json["address"] as? [String: AnyObject]
address = Address(json: addressJson),
...
else {
return nil
}
let obj = UserProfile(id: id, name: name, userName: userName, email: email, address: address, phone: phone, website: website, company: company)
print(address) // output: "street: Kulas Light, city: Gwenborough"
}