使用swift和Alamofire使用Array和字典子数组发送JSON

时间:2017-09-06 12:15:21

标签: json swift alamofire

我想用字典数组发出一个post请求,并且还有字典的子数组,例如:

{
"Title": "You have a new gift from DevPro1034", 
"StartDate": "2017-09-05 22:19:48", 
"EndDate": "2017-09-07 22:19:48", 
"ProviderId": "e134d34d-778c-4163-8935-a4844ea519fa",
"FeatureDetails": [
{ 
"CategoryId": 22, 
"ProviderFeatureDetailId": 10588, 
"CustomServices": 
[
  { 
     "ProviderCustomFeatureDetailId": 13247, 
     "InsiderPrice": 5.0     
  }
]

}
], 
"CurrentDate": "2017-07-26T00:00:00", 
"InsiderOfferSlots": 
   [
  { 
   "AppointmentDate": "2017-09-05 22:19:48", 
   "StartTime": "2017-09-05T09:00:00"
  }
 ], 
"CampaignDetails": { 
"ProviderId": "e134d34d-778c-4163-8935-a4844ea519fa", 
"CampaignMessage": "Hi [First_Name],\nThis is DevPro1034. We have a few last minute offers. See availability.\n\nCampaignLink\n\nReply STOP to opt out", 
"ScheduleDate": "2017-07-27T00:00:00", 
"EmailTextContent": "" 

}
} 

这是我的代码,我正在准备一个帖子请求:

        var featureDetailArray = [[String:AnyObject]]()
    var customeServices = [[String:AnyObject]]()


    for campaign in camUser.getAllCustomServices{

        if campaign.insiderPrice != 0 {

            let param = ["ProviderCustomFeatureDetailId" :campaign.providerCustomFeatureDetailId, "InsiderPrice": campaign.insiderPrice] as [String : Any]

                customeServices.append(param as [String : AnyObject])

            let parameter = ["CategoryId" :campaign.categoryId,
                             "ProviderFeatureDetailId": campaign.providerFeatureDetailId,
                             "CustomServices": customeServices
                ] as [String : Any]
            featureDetailArray.append(parameter as [String : AnyObject])
        }
    }

    // InsiderSlots

    var appointmentArray = [[String:AnyObject]]()


    for appintmentArray1 in camUser.startTimeDay1{

        let parameter = ["StartTime" :appintmentArray1,
                         "AppointmentDate": camUser.appointmentDate1
                         //"ProviderUserId": AuthService.instance.userId
            ] as [String : Any]
        appointmentArray.append(parameter as [String : AnyObject])
    }

    for appintmentArray2 in camUser.startTimeDay2{

        let parameter = ["StartTime" :appintmentArray2,
                         "AppointmentDate": camUser.appointmentDate2
                         //"ProviderUserId": AuthService.instance.userId
            ] as [String : Any]
        appointmentArray.append(parameter as [String : AnyObject])
    }

    for appintmentArray3 in camUser.startTimeDay3{

        let parameter = ["StartTime" :appintmentArray3,
                         "AppointmentDate": camUser.appointmentDate3
                         //"ProviderUserId": AuthService.instance.userId
            ] as [String : Any]
        appointmentArray.append(parameter as [String : AnyObject])
    }


    let campaignDetail = ["ProviderId": AuthService.instance.userId, "ScheduleDate":camUser.startDate,"CampaignMessage" : "Hi we are creating campaign","EmailTextContent": ""] as [String : Any]



    let param : [String:Any] = [
        "Title": "You have a new gift from DevPro1034" as AnyObject,
        "StartDate": camUser.startDate as AnyObject,
        "EndDate" : camUser.endDate as AnyObject,
        "ProviderId": AuthService.instance.userId as AnyObject,
        "FeatureDetails": featureDetailArray as AnyObject,
        "InsiderOfferSlots" : appointmentArray as AnyObject,
        "CampaignDetails": campaignDetail as AnyObject
    ]

    let jsonData = try! JSONSerialization.data(withJSONObject: param, options: [])
    print(jsonData)
    let json = try! JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
    print(json)


    Alamofire.request(CREATE_CAMPAIGN, method: .post, parameters: param, encoding:JSONEncoding.default, headers: HEADER).responseJSON { (response) in
        print(param)

我看到很多与我的问题相关的问题和答案,但还没有找到任何解决方案,这里是我得到初步帮助的链接link Onelink Two

0 个答案:

没有答案