我无法做出嵌套的字典,请帮帮我
{
"OneStopFlight" : "false",
"TokenId" : "86839fb9-ed93-46d3-b480-c58dbc441838",
"AdultCount" : "1",
"InfantCount" : "0",
"JourneyType" : "1",
"EndUserIp" : "192.168.10.10",
"DirectFlight" : "false",
"Segments" : [
{
"PreferredDepartureTime" : "2018-06-27T00:00:00",
"Origin" : "PNQ",
"EndUserIp" : "192.168.10.10",
"Destination" : "VNS",
"FlightCabinClass" : "1"
}
],
"ChildCount" : "0"
}
答案 0 :(得分:0)
如果您要创建如上所示的字典,则可以按如下所示轻松创建它
let segmentDict : [[String:Any]] = [
[
"PreferredDepartureTime" : "2018-06-27T00:00:00",
"Origin" : "PNQ",
"EndUserIp" : "192.168.10.10",
"Destination" : "VNS",
"FlightCabinClass" : "1"
]
]
let paramDict : [String:Any] = [
"OneStopFlight" : "false",
"TokenId" : "86839fb9-ed93-46d3-b480-c58dbc441838",
"AdultCount" : "1",
"InfantCount" : "0",
"JourneyType" : "1",
"EndUserIp" : "192.168.10.10",
"DirectFlight" : "false",
"Segments" : segmentDict,
"ChildCount" : "0"
]
let segmentWithMultipleDict : [[String:Any]] = [
[
"PreferredDepartureTime" : "2018-06-27T00:00:00",
"Origin" : "PNQ",
"EndUserIp" : "192.168.10.10",
"Destination" : "VNS",
"FlightCabinClass" : "1"
],
[
"PreferredDepartureTime" : "2018-06-27T00:00:00",
"Origin" : "PNQ",
"EndUserIp" : "192.168.10.10",
"Destination" : "VNS",
"FlightCabinClass" : "1"
]
]
如果您想对此类数据进行出队
struct dequeResponse
{
var OneStopFlight : String = ""
var TokenId : String = ""
var AdultCount : String = ""
var InfantCount : String = ""
var JourneyType : String = ""
var EndUserIp : String = ""
var DirectFlight : String = ""
var Segments : [segment] = []
var ChildCount : String = ""
func getDataFromJSON(_ data: [String:Any]) -> dequeResponse{
var myResponseValues = dequeResponse()
if let value = data["OneStopFlight"] as? String {
myResponseValues.OneStopFlight = value
}
if let value = data["TokenId"] as? String {
myResponseValues.TokenId = value
}
if let value = data["AdultCount"] as? String {
myResponseValues.AdultCount = value
}
if let value = data["InfantCount"] as? String {
myResponseValues.InfantCount = value
}
if let value = data["JourneyType"] as? String {
myResponseValues.JourneyType = value
}
if let value = data["EndUserIp"] as? String {
myResponseValues.EndUserIp = value
}
if let value = data["DirectFlight"] as? String {
myResponseValues.DirectFlight = value
}
if let value = data["Segments"] as? [[String:Any]] {
let newSegment = segment()
myResponseValues.Segments = newSegment.getDataFromJSON(value)
}
if let value = data["ChildCount"] as? String {
myResponseValues.ChildCount = value
}
return myResponseValues
}
}
struct segment {
var PreferredDepartureTime : String?
var Origin : String?
var EndUserIp : String?
var Destination : String?
var FlightCabinClass : String?
func getDataFromJSON(_ data: [[String:Any]]) -> [segment] {
var segmentAray = [segment]()
for i in 0..<data.count {
var myCurrentSegment = segment()
if let value = data[i]["PreferredDepartureTime"] as? String {
myCurrentSegment.PreferredDepartureTime = value
}
if let value = data[i]["Origin"] as? String {
myCurrentSegment.Origin = value
}
if let value = data[i]["EndUserIp"] as? String {
myCurrentSegment.EndUserIp = value
}
if let value = data[i]["Destination"] as? String {
myCurrentSegment.Destination = value
}
if let value = data[i]["FlightCabinClass"] as? String {
myCurrentSegment.FlightCabinClass = value
}
segmentAray.append(myCurrentSegment)
}
return segmentAray
}
}
用法
var jsonObject = dequeResponse()
jsonObject = jsonObject.getDataFromJSON(paramDict)
print(jsonObject.Segments)
注意:如果这不符合您的要求,请更详细地编辑问题