Alamofire Parameters - NSDictionary is not convertible to [String : AnyObject] - ios

So I am trying to send a json object as parameters with Alamofire. I think I have followed example in their documentation but I get this error with the method call.
#IBAction func saveButton(sender: AnyObject) {
var url = "http://b857aaa4.ngrok.io/recruit"
let parameters = [
"_id" : firstNameTextField.text,
"alt_email" : "",
"forename" : firstNameTextField.text,
"phone" : recruteePhone.text,
"recruiter" : [
"comments" : "",//TODO comments
"date_met" : "",//TODO tdoays date
"email" : "",//TODO recruiter email
"event_name" : ""//TODO event name
],
"surname" : lastNameTextField.text
]
Alamofire.request(.PUT, url, parameters: parameters, encoding: .JSON)
.responseJSON { (req, res, json, error) in
if(error != nil) {
NSLog("Error: \(error)\n")
} else {
var json = JSON(json!);
println("success");
}
}

Declare parameters as
let parameters: [String: AnyObject] = [
and it compiles okay.

Related

Send JSON with array of objects as parameter in Alamofire with swift

I am very new with Swift and Alamofire, what I want to accomplish is to send a data structure, like this:
{
"name" : "Test name",
"intention" : "Purpose of practice test",
"id_frequency" : "1",
"member": [
{
"id_member" : "1",
"email" : "member1#gmail.com",
"id_member_type" : 1
},
{
"id_member" : "4",
"email" : "member2#gmail.com",
"id_member_type" : 3
},
{
"id_member" : "7",
"email" : "member3#gmail.com",
"id_member_type" : 3
},
{
"id_member" : "5",
"email" : "member4#gmail.com",
"id_member_type" : 3
},
{
"id_member" : "6",
"email" : "member5#gmail.com",
"id_member_type" : 3
}
]
}
The way I am proceeding to structure the desired json, is as follows:
var membersArray = [AnyObject]()
for i in 0..<members.count {
let json: [String: Any] = [
"id_member": members[i].idMember!,
"email": members[i].email!,
"id_member_type": "\(Int(members[i].idMemberType)!)",
]
membersArray.append(json as AnyObject)
}
let jsonMembers = JSON(membersArray)
let jsonObject: [String: Any] = [
"member" : jsonMembers,
"name": name!,
"intention": intention!,
"id_frequency": frequency!
]
let jsonUpdate = JSON(jsonObject)
With this structured json, lines above (jsonUpdate). I proceed to execute the webService.
WevServices.createRequest(requestInfo: jsonUpdate) { (result) in
print(result)
}
My webservice method, looks like this:
static func createRequest(requestInfo: JSON, completion: #escaping (_ result: String) -> Void){
let url = URL(string: "http://ws.someURL.com/CreateRequest")
let parameters: [String : Any] = ["name" : "\(requestInfo["name"])", "intention" : "\(requestInfo["intention"])", "id_frequency" : "\(requestInfo["id_frequency"])", "member": requestInfo["member"]]
Alamofire.request(url!, method: .post, parameters: parameters, encoding: URLEncoding.httpBody).responseString { response in
print(response)
}
}
I'm get an error from the server that the send data would not be correct.
Note: My application use Lumen as backend.
The best way for doing what you need is :
func JSONToString(json: [String : String]) -> String?{
do {
let mdata = try JSONSerialization.data(withJSONObject: json, options: JSONSerialization.WritingOptions.prettyPrinted) // json to the data
let convertedString = String(data: mdata, encoding: String.Encoding.utf8) // the data will be converted to the string
print("the converted json to string is : \(convertedString!)") // <-- here is ur string
return convertedString!
} catch let myJSONError {
print(myJSONError)
}
return ""
}
and for alamofire request do this :
static func createRequest(requestInfo: [String :String], completion: #escaping (_ result: String) -> Void){
let url = URL(string: "http://ws.someURL.com/CreateRequest")
let parameters: [String : Any] = ["request" : JSONToString(json : requestInfo)!]
Alamofire.request(url!, method: .post, parameters: parameters, encoding: URLEncoding.httpBody).responseString { response in
print(response)
}

Alamofire .post api error: Code=3840 "Invalid value around character 1

Using Alamofire for .post api, api giving data in postman but not in my code. Posting below the code, please guide what wrong i am doing here:
// API calling method:
parameters = [
"Address" : "" as AnyObject,
"Name" : "" as AnyObject,
"ServiceID" : "" as AnyObject,
"Rating" : "" as AnyObject,
"Price" : "" as AnyObject
]
let headers: Dictionary = [
"" : ""
]
print(parameters)
ApiServices.requestPOSTURL(strURL, params: parameters, headers: headers, success:{
(JSONResponse) -> Void in
CommonMethodsClass.hideHUD(targetView: self.view)
print(JSONResponse["message"])
let strMsg = JSONResponse["message"].stringValue
if (JSONResponse["status"].intValue == 1)
{
}
else
{
CommonMethodsClass.showAlertMessage(vc: self, titleStr: "Error!", messageStr: strMsg)
}
}) {
(error) -> Void in
print(error)
CommonMethodsClass.hideHUD(targetView: self.view)
}
// Api request method:
class func requestPOSTURL(_ strURL : String, params : [String : AnyObject]?, headers : [String : String]?, success:#escaping (JSON) -> Void, failure:#escaping (Error) -> Void){
Alamofire.request(strURL, method: .post, parameters: params, encoding: JSONEncoding.default).responseJSON { (responseObject) -> Void in
print(responseObject)
if responseObject.result.isSuccess {
let resJson = JSON(responseObject.result.value as Any)
success(resJson)
}
if responseObject.result.isFailure {
let error : Error = responseObject.result.error!
failure(error)
}
}
}
Error: FAILURE:
responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error
Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character
1." UserInfo={NSDebugDescription=Invalid value around character 1.}))
Update: Response to parse, may also needs to be changed.
{
"status": true,
"message": "",
"data": [
{
"SalonID": "1",
"SalonName": "Affinity",
"SalonEmail": "vay.chaan#th-rce.com",
"SalonPhone": "9999888877",
"SalonMobile": "9999888877",
"SalonAddress": "C-28, Sec-58, India",
"Latitude": "18.5806",
"Longitude": "27.36273",
"Image": null,
"SalonImage": "",
"TimeIntervalminutes": 20,
"AverageRating": 4,
"IsActive": 1
},
{
"SalonID": "3",
"SalonName": "Looks",
"SalonEmail": "rad#th-rce.com",
"SalonPhone": "99998828877",
"SalonMobile": "99998388877",
"SalonAddress": "GP Mall,India",
"Latitude": "",
"Longitude": "",
"Image": null,
"SalonImage": "",
"TimeIntervalminutes": 30,
"AverageRating": 5,
"IsActive": 1
}
]
}
Replace responseJSON with responseString.
You need to convert the string for values. Can you update the string in your question?
You can use this function to convert response string into JSON:
func convertToDictionary(text: String) -> [String: Any]? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
} catch {
print(error.localizedDescription)
}
}
return nil
}
String: let str = "{\"name\":\"James\"}"
USAGE: let dict = convertToDictionary(text: str)
Try responseString instead of responseJSON and it will work
Alamofire.request("URL").responseString { response in
print(response)
if let json = response.result.value {
print("JSON: \(json)")
}
}
Change responseJSON to responseString and it will work :)
Alamofire.request("URL").responseString { response in
print(response)
if let json = response.result.value {
print("JSON: \(json)")
}
}

Converting Dictionary to Json to pass parameters in Alamofire

I have a dictionary with key as username and value as email. Which i would like to send to an api using Alamofire i have no clue how to approach this problem as i am suppose to send multiple users to the api to save at once.
Dictionary
var selectedMembers = [String: String]()
The data saved in this dictionary is appended in a different VC from a table view where we can choose how many users we want to append in the dictionary.
Now i need to convert this dictionary into json formate to send to the api through alamofire.
Json Formate
"users": [
{
"user_email": "abc#gmail.com",
"user_name": "abc"
},
{
"user_email": "abc2#gmail.com",
"user_name": "abc2"
}
]
Alamofire Code
let parameters: Parameters = [
"users" : [
[
"user_name" : "user_name goes here",
"user_email" : "user_email goes here"
]
]
]
Alamofire.request("\(baseURL)", method: .post, parameters: parameters).responseJSON { response in
}
How i solved the Problem
i created a function that prints the the data how i want it and placed it in Alamofire parameters something like this.
var selectedMembers = [String: String]()
var smembers = [AnyObject]()
var selected = [String: String]()
if selectedMembers.isEmpty == false {
for (key, value) in selectedMembers {
selected = ["useremail": key, "catagory": value]
smembers.append(selected as AnyObject)
}
}
let jsonData = try? JSONSerialization.data(withJSONObject: smembers, options: JSONSerialization.WritingOptions())
let jsonString = NSString(data: jsonData!, encoding: String.Encoding.utf8.rawValue)
let parameters: Parameters = [
"users" : jsonString as AnyObject
]
Alamofire.request("\(baseURL)", method: .post, parameters: parameters).responseJSON { response in
}
You just need to make Array of dictionary(user) as of currently you are setting user's value as Dictionary instead of Array.
let parameters: Parameters = [
"users" : [
[
"user_name" : "abc",
"user_email" : "abc#gmail.com"
],
[
"user_name" : "abc2",
"user_email" : "abc2#gmail.com"
]
]
]
Alamofire.request("\(baseURL)", method: .post, parameters: parameters).responseJSON { response in
}
If your web API demands the post data to send in JSON format then below written method is a way to do this.
func calltheAPIToSendSelectedUser () {
let parameters: [String: Any] = [
"users" : [
[
{
"user_name" : "user1_name goes here",
"user_email" : "user1_email goes here"
},
{
"user_name" : "user2_name goes here",
"user_email" : "user2_email goes here"
},
{
"user_name" : "user3_name goes here",
"user_email" : "user3_email goes here"
},
]
]
]
var request = URLRequest(url: URL(string: "YourApiURL")!)
request.httpMethod = "POST"
request.httpBody = try! JSONSerialization.data(withJSONObject: parameters, options: [.prettyPrinted])
Alamofire.request(request).responseJSON { response in
}
}
Here's my solution that worked for invoking POST using Alamofire and passing in an array of dictionary values as the Parameters arg.
Notice the 'User' key which contains a dictionary of values.
let dict: NSMutableDictionary = [:]
dict["facebook_id"] = "1234559393"
dict["name"] = "TestName"
dict["email"] = "TestEmailID"
let params: Parameters = [
"user": dict
]
Alamofire.request("YOURURL", method: .post, parameters: params, encoding: JSONEncoding.default).responseJSON(completionHandler: { (response) in
switch response.result {
case .success:
print("succeeded in making a Facebook Sign up REST API. Now going to update user profile"
break
case .failure(let error):
return
print(error)
}
})
Right at the point where you are adding items to the dictionary, is where you can check for nil values and avoid adding as needed, thus making it dynamic.

Unable to convert this JSON to a string

Please see below the json output
{
"queryLogs" : [
{
"status" : "false",
"query" : {
"contents" : {
"updated" : "",
"id" : 1488199579,
"created" : "",
"patient_count" : 60,
"isactive" : "1",
"status_id" : 0,
"starttime" : "",
"queue_status_id" : 0,
"date_consult" : ""
},
"conditions" : "{}"
},
"tableName" : "consultation",
"type" : "I",
"logId" : {
"id" : "261489537666",
"doctorId" : "100"
}
}
]
}
Need to convert above json into below format
{"queryLogs":[{ "logId":{"id":"76148951287","doctorId":"100"},
"tableName":"queue", "type":"I", "query":"{ \"contents\":{
\"patient_name\":\"queryLog Test\", \"status_id\":1,
\"queue_no\":\"6\", \"isactive\":1, \"id\":\"148956612\",
\"mobile\":\"9567969610\", \"updated\":\"2017-03-15 11:31:26
GMT+05:30\", \"created\":\"2017-03-15 11:31:26 GMT+05:30\",
\"consultation_id\":\"1495085636\"}, \"conditions\":{}
}","status":"false"}]}
First code is what i get when i convert the JSON, But how can i get the JSON like the second code.
i used below code to get the first output.
var f = ["queryLogs":[["status":"false","tableName":"consultation","type":"I","logId":ids,"query":logfile]]] as [String : Any]
let JSON = try? JSONSerialization.data(withJSONObject: f,
options:.prettyPrinted)
if let content = String(data: JSON!, encoding:
String.Encoding.utf8) {
print(content)
}
If you want response like that then you need to also make JSON string for your logfile dictionary also.
What you can do is make one extension of Dictionary, so that no need to write same code of JSONSerialization at every place.
extension Dictionary where Key: ExpressibleByStringLiteral {
var jsonString: String? {
guard let data = try? JSONSerialization.data(withJSONObject: self),
let string = String(data: data, encoding: .utf8) else {
return nil
}
return string
}
}
Now use this extension to get JSON string from your dictionaries.
let id‌​s = ["id" : "261489537666", "doctorId" : "100"]
let logfile = [
"contents" : [
"updated" : "",
"id" : 1488199579,
"created" : "",
"patient_count" : 60,
"isactive" : "1",
"status_id" : 0,
"starttime" : "",
"queue_status_id" : 0,
"date_consult" : ""
],
"conditions" : "{}"
] as [String : Any]
if let queryLogString = logfile.jsonString {
let f = ["queryLogs":[["status":"false","tableName":"consultation","‌​type":"I","logId": id‌​s,"query":queryLogString]]] as [String : Any]
if let content = f.jsonString {
print(content)
}
}
Output:
{"queryLogs":[{"status":"false","query":"{\"contents\":{\"updated\":\"\",\"id\":1488199579,\"created\":\"\",\"patient_count\":60,\"isactive\":\"1\",\"status_id\":0,\"starttime\":\"\",\"queue_status_id\":0,\"date_consult\":\"\"},\"conditions\":\"{}\"}","tableName":"consultation","‌​type":"I","logId":{"id":"261489537666","doctorId":"100"}}]}
once try like,
let data = try? JSONSerialization.data(withJSONObject: dic, options: JSONSerialization.WritingOptions(rawValue: 0))
if let content = String(data: data!, encoding:
String.Encoding.utf8) {
print(content)
}

Loop through JSON response from Alamofire

I am using xcode 7.2 and swift 2.1.1. using Alamofire for server communication. I have a tableview that display's dynamic data.Data includes username , useremail , profilePicture etc. I Tried to implement this code from stackoverflow. But I am getting a warning message says Cast from 'JSON' to unrelated type '[Dictionary]' always fails My json response is
{
"JsonRequestBehavior" : 1,
"MaxJsonLength" : null,
"ContentType" : null,
"Data" : {
"_id" : "5658444778a7531f4c79c23d",
"Photo" : "",
"AllowSharing" : "YES",
"MemberCount" : 5,
"Users" : [
{
"_id" : "5658443478a7531f4c79c23c",
"Photo" : "",
"MembershipDate" : "0001-01-01T00:00:00",
"MiddleName" : null,
"FirstName" : "Gohan",
"LastName" : null,
"Email" : "gohan#gmail.com"
},
{
"_id" : "566ea5f1dfead62350cf0fad",
"Photo" : "",
"MembershipDate" : "0001-01-01T00:00:00",
"MiddleName" : null,
"FirstName" : null,
"LastName" : null,
"Email" : "sachin#gmail.com"
}
],
"MembershipDate" : "2015-12-14T12:03:12.819Z",
"CreatedBy" : "5658443478a7531f4c79c23c"
},
"ContentEncoding" : null,
"RecursionLimit" : null
}
How can I loop through Users in JSON response ?
EDIT as per JohnyKutty's answer I tried the same code in my Project. The code for the same is
Alamofire.request(.GET,App.AppHomeURL() + "Load_Group", parameters: ["groupid":"\(groupId)"]).responseJSON
{
response in
print("\(response.data)")
switch response.result
{
case .Success(let _data):
let jsonData = JSON(_data)
print("Admin Response : \(jsonData)")
do
{
let json = try NSJSONSerialization.JSONObjectWithData(_data as! NSData, options: .AllowFragments) as! NSDictionary
if let DataObject = json["Data"] as? NSDictionary
{
if let users = DataObject["Users"] as? [NSDictionary]
{
for user in users
{
print("User id : \(user["_id"])")
}
}
}
}
catch let error as NSError
{
print(error.localizedDescription)
}
in this line let json = try NSJSONSerialization.JSONObjectWithData(_data as! NSData, options: .AllowFragments) as! NSDictionary
At first I used "_data" and then Xcode suggested a change and it changed from _data to _data as! NSData.
Your son structure is like following
JSON(Dictionary) -> Data(Dictionary) -> Users(Array of Dictionaries). So first you should pick the Users array from raw json then iterate through it.
Since alamofire is already serializing your response, No need to use JSONSerializer again, I am updating my answer.
UPDATE
Try this code inside the case
if let DataObject = _data["Data"] as? NSDictionary {
if let Users = DataObject["Users"] as? [NSDictionary] {
for user in Users {
print(user["_id"],user["MembershipDate"],user["FirstName"],user["Email"], separator: " ", terminator: "\n")
}
}
}
Full code:
Alamofire.request(.GET,App.AppHomeURL() + "Load_Group", parameters: ["groupid":"\(groupId)"]) .responseJSON { response in
switch response.result {
case .Success(let _data):
print(_data)
if let DataObject = _data["Data"] as? NSDictionary {
if let Users = DataObject["Users"] as? [NSDictionary] {
for user in Users {
print(user["_id"],user["MembershipDate"],user["FirstName"],user["Email"], separator: " ", terminator: "\n")
}
}
}
default:
break;
}
}

Resources