Reputation: 2408
I need to make a request body looking like this:
{
"accepted" : [
{
"deposit" : 2000,
"name" : "Tuxedo",
"rent" : 100,
"id" : 3,
"favourited_by" : [
],
"tag_id" : 21,
"status" : "unknown",
"image_url" : "https:\/\/www.moss.co.uk\/images\/extralarge\/965549415_01.jpg",
"addresses" : [
]
},
{
"deposit" : 3000,
"name" : "ps4",
"rent" : 50,
"id" : 2,
"favourited_by" : [
],
"tag_id" : 16,
"status" : "unknown",
"image_url" : "http:\/\/www.spokeslabs.com\/jstone\/ps4_images\/ps4-hrdware-large18.jpg",
"addresses" : [
]
},
{
"deposit" : 1000,
"name" : "Electric drill",
"rent" : 100,
"id" : 1,
"favourited_by" : [
],
"tag_id" : 11,
"status" : "unknown",
"image_url" : "https:\/\/static.independent.co.uk\/s3fs-public\/styles\/story_medium\/public\/thumbnails\/image\/2016\/06\/20\/12\/ryobi-rpd800-k-percussion-d.jpg",
"addresses" : [
]
}
],
"rejected" : [
],
"address" :
{
"city" : "Hong Kong",
"lng" : "114.162699999745",
"country" : "Hong Kong",
"street" : "Barker Road",
"id" : "0",
"label" : "Home",
"lat" : "22.269837686727"
}
}
Unfortunantly I'm sending this:
{
"accepted" : [
{
"deposit" : 2000,
"name" : "Tuxedo",
"rent" : 100,
"id" : 3,
"favourited_by" : [
],
"tag_id" : 21,
"status" : "unknown",
"image_url" : "https:\/\/www.moss.co.uk\/images\/extralarge\/965549415_01.jpg",
"addresses" : [
]
},
{
"deposit" : 3000,
"name" : "ps4",
"rent" : 50,
"id" : 2,
"favourited_by" : [
],
"tag_id" : 16,
"status" : "unknown",
"image_url" : "http:\/\/www.spokeslabs.com\/jstone\/ps4_images\/ps4-hrdware-large18.jpg",
"addresses" : [
]
},
{
"deposit" : 1000,
"name" : "Electric drill",
"rent" : 100,
"id" : 1,
"favourited_by" : [
],
"tag_id" : 11,
"status" : "unknown",
"image_url" : "https:\/\/static.independent.co.uk\/s3fs-public\/styles\/story_medium\/public\/thumbnails\/image\/2016\/06\/20\/12\/ryobi-rpd800-k-percussion-d.jpg",
"addresses" : [
]
}
],
"rejected" : [
],
"address" : [
{
"city" : "Hong Kong",
"lng" : "114.162699999745",
"country" : "Hong Kong",
"street" : "Barker Road",
"id" : "0",
"label" : "Home",
"lat" : "22.269837686727"
}
]
}
The difference is in the last section of the JSON. What I'm sending contains an array of addresses but I want to be sending just contains one address object.
This request body gets created the following way:
var parameters = [String:[AnyObject]]()
parameters["rejected"] = rejectedItemsArray as [AnyObject]
parameters["accepted"] = acceptedItemsArray as [AnyObject]
parameters["address"] = addressArray as [AnyObject]
let request = NSMutableURLRequest(URL: NSURL(string: urlString)!)
request.setValue(self.token!, forHTTPHeaderField: Constant.tokenUserDefaultsKey)
request.HTTPMethod = "POST"
do {
let data = try NSJSONSerialization.dataWithJSONObject(parameters, options: .PrettyPrinted)
let requestBodyString = String(data: data, encoding: NSUTF8StringEncoding)
NSLog("Request Body: %@", requestBodyString!)
request.HTTPBody = data
} catch ( _) {
NSLog("Failed to encode json for Post Items")
}
How do I fix it?
I want to have 2 arrays in my JSON:
1 - "accepted" 2 - "rejected"
And 1 single object in my JSON:
1 - "address"
Upvotes: 2
Views: 407
Reputation: 79696
parameters["address"] = addressArray as [Any]
Here (in this line of code) you are assigning array instance. Create a JSON object (Dictionary) of 'addressArray' and then assign it to parameter["address"]
e.g.
var addresArray = [String : Any]()
//Store json values/information in it and then
parameters["address"] = addressArray as [String : Any]
Upvotes: 0
Reputation: 72420
Problem is in this line parameters["address"] = addressArray as [AnyObject]
. You are setting Array
with address
key instead of that you need to set dictionary. So create addressDic like this.
let adddressDic = ["city" : "Hong Kong", "lng" : "114.162699999745", "country" : "Hong Kong",
"street" : "Barker Road", "id" : "0", "label" : "Home", "lat" : "22.269837686727"]
Now set this Dictionary
with address
key
parameters["address"] = adddressDic
Edit: You need to also change the declaration of parameters like this.
var parameters = [String:AnyObject]()
Upvotes: 1