【发布时间】:2019-10-03 06:45:30
【问题描述】:
我想向服务器发送一个 JSON,其中包含用户的详细信息和用户选择的选项。目前我只是将它们保存在简单的数组中。
我想创建一个 JSON 并将其发送到服务器。通常我可以使用 Alamofire 在 Parameter 上添加每个字段。但是这里我有个问题是,字段的数量取决于用户数量(或乘客数量)。
我以前没有做过这种类型,我是 iOS 开发的新手。
{
"contactPerson": {
"email": {
"email": "memine@gmail.com",
"markedForSendingRezInfo": false,
"preferred": false,
"shareMarketInd": false
},
"personName": {
"givenName": "Me",
"shareMarketInd": false,
"surname": "Mine"
},
"phoneNumber": {
"countryCode": "93",
"markedForSendingRezInfo": false,
"preferred": false,
"shareMarketInd": false,
"subscriberNumber": "123456789"
},
"shareMarketInd": false,
"useForInvoicing": false
},
"curr": "USD",
"flight1": "{fligh1Info}",
"flight2": "{flight2Info}",
"passengers": [{
"accompaniedByInfant": true,
"birthDate": {
"day": 6,
"fractionalSecond": 0.000,
"hour": 0,
"minute": 0,
"month": 9,
"orig_day": 6,
"orig_fracSeconds": 0.000,
"orig_hour": 0,
"orig_minute": 0,
"orig_month": 9,
"orig_second": 0,
"orig_timezone": 330,
"orig_year": 1991,
"second": 0,
"timezone": 330,
"year": 1991
},
"hasStrecher": false,
"parentSequence": 0,
"passengerTypeCode": "ADLT",
"personName": {
"givenName": "some",
"nameTitle": "Mr.",
"shareMarketInd": false,
"surname": "guy"
},
"requestedSeatCount": 1,
"shareMarketInd": false,
"unaccompaniedMinor": false
}, {
"accompaniedByInfant": false,
"birthDate": {
"day": 10,
"fractionalSecond": 0.000,
"hour": 0,
"minute": 0,
"month": 10,
"orig_day": 10,
"orig_fracSeconds": 0.000,
"orig_hour": 0,
"orig_minute": 0,
"orig_month": 10,
"orig_second": 0,
"orig_timezone": 330,
"orig_year": 2010,
"second": 0,
"timezone": 330,
"year": 2010
},
"hasStrecher": false,
"parentSequence": 0,
"passengerTypeCode": "CHLD",
"personName": {
"givenName": "some",
"shareMarketInd": false,
"surname": "child"
},
"requestedSeatCount": 1,
"shareMarketInd": false,
"unaccompaniedMinor": false
}, {
"accompaniedByInfant": false,
"birthDate": {
"day": 1,
"fractionalSecond": 0.000,
"hour": 0,
"minute": 0,
"month": 1,
"orig_day": 1,
"orig_fracSeconds": 0.000,
"orig_hour": 0,
"orig_minute": 0,
"orig_month": 1,
"orig_second": 0,
"orig_timezone": 330,
"orig_year": 2018,
"second": 0,
"timezone": 330,
"year": 2018
},
"hasStrecher": false,
"parentSequence": 0,
"passengerTypeCode": "INFT",
"personName": {
"givenName": "some",
"shareMarketInd": false,
"surname": "kid"
},
"requestedSeatCount": 1,
"shareMarketInd": false,
"unaccompaniedMinor": false
}],
"pwd": "password",
"requestPurpose": "MODIFY_PERMANENTLY_AND_CALC",
"unm": "username"
}
这是我的 JSON。这里flight1flight2 的数量取决于用户选择的航班数量。
passengers 内的对象数量取决于用户选择的乘客数量。
这部分我该怎么做?
【问题讨论】:
-
您需要创建包含上述结构的
NSDictionary并将其转换为json。检查这个link -
@PratikPrajapati 好的,但是我如何将航班数量和乘客数量添加到 NSDictionary 中的确切位置? (因为这两种变化都取决于用户的选择)
-
@PratikPrajapati 你不需要
NSDictionary,你需要Swift Dictionary ([String:Any]) -
创建一个数组来保存像
[ "flight1": "{fligh1Info}", "flight2": "{fligh2Info}"]这样的值,并在用户选择时添加/删除这个值,然后将此数据添加到您的主Dictionary。 -
@PratikPrajapati 哦,谢谢。一个问题,如果我只是将它移动到这样的数组中,我怎么知道这部分被添加到了 json 中的正确位置?(或字典)?
标签: ios json swift alamofire swifty-json