【发布时间】:2018-04-13 10:52:00
【问题描述】:
我有如下所示的 Json 服务器响应数据,我使用模型类获取了所有键值对,但无法仅获取一个键值对街道有人可以帮助我如何获取街道键值对吗?
这是我的 Json 数据
{
"address": {
"city": "chennai",
"firstname": "sommesh",
"lastname": "s",
"email": "test@gmail.com",
"postcode": "43",
"street": [
"[No: 14; 8th cross street]"
],
"country_id": "US",
"region_code": "CA",
"region_id": "12",
"telephone": "8756467654",
"sameAsBilling": 1,
"region": "California",
"prefix": "",
"company": "Test Company"
}
}
这是我的模型类代码
struct GuestAddress {
let id : Int
let region : String
let regionId : Any
let regionCode : String
let countryId : String
let company : String
let telephone : Any
let postCode : Any
let city : String
let firstName : String
let lastName : String
let email : String
let sameAsBilling : Any
let saveInAddressBook : Any
var street : [String]
init(dict : [String:Any]) {
self.id = dict["id"] as! Int
self.region = dict["region"]! as! String
self.regionId = dict["region_id"]!
self.regionCode = dict["region_code"]! as! String
self.countryId = dict["country_id"]! as! String
self.company = dict["company"]! as! String
self.telephone = dict["telephone"]! as! String
self.postCode = dict["postcode"]!
self.city = dict["city"]! as! String
self.firstName = dict["firstname"]! as! String
self.lastName = dict["lastname"]! as! String
self.email = dict["email"]! as! String
self.sameAsBilling = dict["same_as_billing"]!
self.saveInAddressBook = dict["save_in_address_book"]!
let guestStreet = dict["street"] as! [String]
var streetArr = street
for item in guestStreet {
streetArr.append(item)
}
street = streetArr
}
}
【问题讨论】:
-
你能澄清你的要求吗?你说“只得到一个键值对街道”。但是什么是“键值对街”,你只想得到一个是什么意思?
-
我的 Json 数据中的街道键值对我无法使用上面显示的代码获得@RayToal
-
您只需检查我在上面发布的图片,那是我的邮递员图片@RayToal
-
street是一个json数组,不能当成字符串。
-
那么所有其他的都有效吗?比如
email、lastName等等?而你就是无法获得street?