这是我的json值
"end_location" = {
"location_name" = "";
pLatitude = "<null>";
pLongitude = "<null>";
};
"start_location" = {
"location_name" = "<null>";
pLatitude = "<null>";
pLongitude = "<null>";
};
我想将此json的数据设置为
下面的类 class busLocationAndDetails {
class bus_location {
var pLatitude:String!
var pLongitude:String!
}
class busDeatils {
var busStatus:String!
var busName:String!
}
}
我将直接从班级访问数据 busLocationAndDetails.bus_location.pLatitude 这必须从json返回数据
谢谢
答案 0 :(得分:1)
由于您要使用这些类来存储数据,因此最好使用struct而不是类。
struct BusLocationAndDetails {
var busLocation : BusLocation
var busDetails : BusDetails
init(dictionary : [ String: AnyObject]){
let latitude = dictionary["pLatitude"] as! String
let longitude = dictionary["pLongitude"] as! String
busLocation = BusLocation(lat: latitude, long: longitude)
busDetails = BusDetails(status: "", name: "")
}
struct BusLocation {
var pLatitude:String!
var pLongitude:String!
init( lat: String, long :String ){
pLatitude = lat
pLongitude = long
}
}
struct BusDetails {
var busStatus:String!
var busName:String!
init( status: String, name :String ){
busStatus = status
busName = name
}
}
}
然后你可以保存这些值
var busLocationAndDetails = BusLocationAndDetails(dictionary: yourJson)
//yourJson value should be the values of the key end_location
然后你可以获得像
这样的值var latitude = busDetails.busLocation.pLatitude