如何使用Alamofire和Swift 3使用JSON发布帖子请求

时间:2017-08-31 13:55:53

标签: ios json swift3 alamofire

这是我想在params中使用的json。

"Title":"You have a new gift from DevPro1034",
"StartDate":"2017-07-27T00:00:00",
"EndDate":"2017-07-29T00:00:00",
"ProviderId":"f6b80d00-410f-46c0-9087-167ecf853441”
"FeatureDetails":[
   {

      "CategoryId":21,    
      "ProviderFeatureDetailId":10803,
      "CustomServices":[
         {
            "ProviderCustomFeatureDetailId":13284,
            "InsiderPrice":8.0
         }
      ],
      "SortOrder":null,
      "CategorySortOrder":null
   },
   {
      "CategoryId":21,
      "ProviderFeatureDetailId":10803,
      "CustomServices":[
         {
            "ProviderCustomFeatureDetailId":13285,
            "InsiderPrice":15.0
         }
      ],
      "SortOrder":null,
      "CategorySortOrder":null
   }
],
"OldFavoriteOfferId":0,
"CurrentDate":"2017-07-26T00:00:00",
"InsiderOfferSlots":[
   {
      "ProviderAppointmentsId":"00000000-0000-0000-0000-000000000000",
      "AppointmentDate":"2017-07-27T00:00:00",
      "StartTime":"2017-07-27T08:00:00",
      "EndTime":"0001-01-01T00:00:00",
      "ProviderUserId":"00000000-0000-0000-0000-000000000000"
   },
   {
      "ProviderAppointmentsId":"00000000-0000-0000-0000-000000000000",
      "AppointmentDate":"2017-07-27T00:00:00",
      "StartTime":"2017-07-27T08:00:00",
      "EndTime":"0001-01-01T00:00:00",
      "ProviderUserId":"00000000-0000-0000-0000-000000000000"
   }


],
"CampaignDetails":{
   "CampaignId":0,
   "ProviderId":"00000000-0000-0000-0000-000000000000",
   "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",
   "IsSMS":true,
   "IsEmail":true,
   "IsNotification":true
},

}

这是我用于POST请求的代码。

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

    for campaign in camUser.getAllCustomServices{

        if campaign.insiderPrice != 0 {

            let customeServices = [["ProviderCustomFeatureDetailId" :campaign.providerCustomFeatureDetailId, "InsiderPrice": campaign.insiderPrice]]

            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", "IsSMS":camUser.isText, "IsEmail":camUser.isEmail, "IsNotification": camUser.isNotification] as [String : Any]



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

    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)
        if response.result.error == nil {
            NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
             print(param)
             print(response)
             print(response.data ?? "")
            print("Request: \(String(describing: response.request!))")
            let json = JSON(data: response.data!)
            let message = json["Message"].stringValue

            print(message)
            print(json)
            completion(json)
        }else{
            //completion(false)
            debugPrint(response.result.error as Any)
        }


    }


}

我看过很多评价答案,但没有一个能解决我的问题。

Alamofire: Send JSON with Array of Dictionaries

最初我从这个链接得到了想法,实际上我想发送一个字典数组,里面有另一个字典数组,这就是为什么我很困惑如何处理这种向服务器端发送数据的大量请求请帮忙。

0 个答案:

没有答案