JSONSerialization.jsonObject returning nil? - ios

Note: I am on step 2.3 here, I am working to integrate stripe with firebase.
I have the following guard let, which will fail. It fails because of the json const.
guard let data = data, let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any], let accountURLString = json["url"] as? String, let accountURL = URL(string: accountURLString) else {
// handle error
print(": jsonfdshkfdbsh :")
return
}
Unfortunately the following declaration within it fails:
let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [String : Any]
What is wrong with the line? How can I make it work?
Update: (What I have tried but has not worked)
let data = data as Data?
print(data, " datavar")//has a value (not nil)
let json = try? JSONSerialization.jsonObject(with: data!, options: []) as? [String : Any]
print(json, " jsonvar") //nil
let accountURLString = json!["url"] as? String
let accountURL = URL(string: accountURLString!)
Update 2:
I've been wondering and investigating whether the problem may be that request variable path may not be valid(?). However, my testing, with a value I know exists in the database, has not yielded (worked) any results.
if let url = URL(string: backendAPIBaseURL)?.appendingPathComponent("stripe_customers/IYNpofaWUFXfobmFRLMLIEZXxqN2") {// usually the string is: "onboard-user"
var request = URLRequest(url: url)
request.httpMethod = "POST"
print(URLSession.shared.dataTask(with: request), "<-- ay un problem?")
print(request, " this was the request value")
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
print("does thsi run??? ", data)
let data: Data = data!// as Data?
print(data, " ttekjfdsjklfhdas")
//...

The problem turned out to be associated with Update 2. The request was the problem.
I ended changing the code to the following, which worked:
func createStripeConnectAccount(uid: String, completion: #escaping(String?, String?) -> Void) { //accountID, Error
let parameters: [String:Any] = [:]
let url = "https://us-central1-name-fhdskj.cloudfunctions.net/createConnectAccount"
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:]).responseJSON { response in
switch response.result {
case .success(let dict):
print(dict)
let successDict: [String: Any?] = dict as! [String: Any?]
let body = successDict["body"] as! [String: Any?]
let acctNum = body["success"] as! String
print(acctNum, "<-- link did it twerk?")
completion(acctNum, nil)
case .failure(let error):
print(error.localizedDescription)
completion(nil, error.localizedDescription)
}
}
}
func createAccountLink(accountID: String, completion: #escaping(String?, String?) -> Void) { //url, Error
let parameters: [String:Any] = ["accountID": accountID]
let url = "https://us-central1-name-fdsad.cloudfunctions.net/createStripeAccountLink"
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:]).responseJSON { response in
switch response.result {
case .success(let dict):
print(dict)
let successDict: [String: Any?] = dict as! [String: Any?]
let body = successDict["body"] as! [String: Any?]
let link = body["success"] as! String
print(link, "<-- link did it twerk?")
completion(link, nil)
case .failure(let error):
print(error.localizedDescription)
completion(nil, error.localizedDescription)
}
}
}
You'll have to integrate Alomofire Pod.

Related

Issue while sending array of dictionary in swift

I am creating array of dictionaries and then passing that in paramDictionary and sending to server but I get responseStatus code 422. I am using Alamofire 5.
Here is the structure of param which I have to send and it successfully working on postman but in app it is always fails
{"check_in": [{"check_in_at":"2020-02-26 03:23:44", "gps_coordinates":"3.1697998046875,101.61672197976593"},
{"check_in_at":"2020-02-26 03:23:45","gps_coordinates":"3.1697998046875,101.61672197976593"}]}
Here is my code
func postCheckInApi(viewController: UIViewController,
completion:#escaping (_ result:SuccessErrorData)->(),
errorHandler:#escaping (_ result:Error,_ statusCode:Int?)->()//error handler
) {
let url = KCheckin
let geoArr = Constant.getSearchLocationHistory() ?? [GeoTaggingEntity]()
var arr = [[String: String]]()
for i in geoArr{
let dict: [String : String] = ["gps_coordinates" : i.gps_coordinates ?? "", "check_in_at" : i.check_in_at]
arr.append(dict)
}
let parameterDictionary = ["check_in": arr] as [String : Any]
print(parameterDictionary)
let headers: HTTPHeaders = [
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": Constant.getBearerToken() ?? ""
]
AF.request(url, method: .post, parameters: parameterDictionary, headers: headers).responseData { response in
switch response.result{
case.success(let data):
do{
let jsonData = try JSONDecoder().decode(SuccessErrorData.self, from: data)
print("Success")
completion(jsonData)
}
catch{
//viewController.navigationController?.popToRootViewController(animated: true)
}
case .failure(let error):
print(error)
}
}
}
You can not add Array as parameter object to your paramDictionary. You need to covert your Array to json string and then add it to you paramDictionary.
For that use below Collection extension to convert your Array to Json String
extension Collection {
func json() -> String? {
guard let data = try? JSONSerialization.data(withJSONObject: self, options: []) else {
return nil
}
return String(data: data, encoding: String.Encoding.utf8)
}
}
How To Use
let jsonStr = yourArray.json()
add this jsonStr to your paramDictionary

Swift 4 - Alamofire post request with httpbody

I have this Alamofire post request like so:
var jsonArrayOfDictionaries = [[AnyHashable: Any]]()
let user = appDelegate.username
let password = appDelegate.password
let url = webservice + "PostTasks"
let credential = URLCredential(user: user!, password: password!, persistence: .forSession)
let headers = ["Accept": "application/json;odata=verbose", "Content-type": "application/json;odata=verbose"]
let jsonData: Data? = try? JSONSerialization.data(withJSONObject: jsonArrayOfDictionaries, options: .prettyPrinted)
print(jsonData!)
//request.httpBody = jsonData
Alamofire.request(url, method: .post, headers: headers).authenticate(usingCredential: credential).responseJSON {
(response) in
switch response.result {
case .success:
if let value = response.result.value {
print(value)
OperationQueue.main.addOperation({
completion(true)
})
}else{
print("There is error in the server response")
completion(false)
}
case .failure (let error):
print("The NTLM request error is: ", error.localizedDescription)
completion(false)
}
}
My question is how do I add the jsonData variable to the httpbody of this request? I have looked into this issue and all the solutions appear to be old. Please help!
This is how jsonArrayOfDictionaries is getting populated:
var jsonArrayOfDictionaries = [[AnyHashable: Any]]()
for i in 0..<cellHolder.count {
var jsonDict = [AnyHashable: Any]()
jsonDict["scheduleTaskID"] = cellHolder[i].scheduleTaskID
jsonDict["task"] = cellHolder[i].task
jsonDict["scheduledDate"] = cellHolder[i].scheduledDate
jsonDict["actualDate"] = cellHolder[i].actualDate
jsonDict["finishedDate"] = cellHolder[i].finishedDate
jsonDict["selected"] = (cellHolder[i].selected) ? 1 : 0
jsonDict["completedBy"] = appDelegate.username
jsonDict["sortOrder"] = cellHolder[i].sortOrder
jsonArrayOfDictionaries.append(jsonDict)
jsonDict = [AnyHashable: Any]()
}
Its in a loop and gets appended.
1. Change
let jsonData: Data? = try? JSONSerialization.data(withJSONObject: jsonArrayOfDictionaries, options: .prettyPrinted)
to
let jsonData = try JSONSerialization.jsonObject(with: jsonArrayOfDictionaries, options: []) as? [String : Any]
2. Add jsonData to your request
Alamofire.request(url, method: .post, headers: headers, parameters: jsonData).authenticate(usingCredential: credential).responseJSON {

How to parse json in swift (convert json string to string)

I don't find a way to parse a simple json object into a string object in swift. I have a network request which gives me this json response:
"\"asdf\""
When I try to parse this into a string in swift it looks like this:
"\"asdf\""
according this documentation from apple I should only need to do this:
Apple Swift documentation
let jsonValue = responseData as? String
But that does not work for me.
I need just asdf as string value.
Can anyone help me out?
Thanks in advance.
EDIT:
Here is my network request code:
let stringUrl = "https://test.fangkarte.de/v1.3/test"
let url = URL(string: stringUrl)!
let request = URLRequest(url: url)
let session = URLSession(configuration: URLSessionConfiguration.default)
let task = session.dataTask(with: request, completionHandler: {(data, response, error) -> Void in
if let data = data {
let json = String(data: data, encoding: String.Encoding.utf8)
let response = response as! HTTPURLResponse
if 200...299 ~= response.statusCode {
callback(true, response.statusCode, json!)
} else {
callback(false, response.statusCode, json!)
}
}
})
task.resume()
The value of the variable json is "\"testString\"" and not "testString"
You could try something like:
func parseJSON(_ data: Data) -> [String: Any]? {
do {
if let json = try JSONSerialization.jsonObject(with: data) as? [String: Any],
let body = json["data"] as? [String: Any] {
return body
}
} catch {
print("Error deserializing JSON: \n\(error)")
return nil
}
return nil
}
To use:
let data = <variable holding JSON>.data(using: .utf8)
let jsonResult = parseJSON(data)
You get a json string so you can try
let jsonstring = "\"asdf\""
let data = jsonstring.data(using: .utf8)
do {
if let str = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as? String {
print(str)
}
}
catch let caught as NSError
{
}

ios - How to wait api response using Alamofire

I want to ask about Semaphore in Alamofire.
I want the app wait for data from the server return success and continue to execute the code after (synchronous type). I use semaphore, but when the api function is called, the app is suspended...
This code is call data from server:
func getAllModels() -> [String] {
var _modelList:[String] = []
let url = BASE_URL + "getAllProductAndModelv2"
let semaphore = DispatchSemaphore(value: 0)
Alamofire.request(url, method:.get, parameters: [:], encoding: JSONEncoding.default).responseJSON { response in
let data = NSData(contentsOf: URL(string: url)!)
do {
if let data = data, let json = try JSONSerialization.jsonObject(with: data as Data) as? [String: Any], let models = json["models"] as? [[String:Any]] {
for model in models {
if let name = model["name"] as? String {
_modelList.append(name)
}
}
}
}catch {
print("error")
}
semaphore.signal()
}
semaphore.wait()
return _modelList
}
And this code is going to get the result:
let api = RestApiManager()
var result:[String] = api.getAllModels()
print(result)
How to relsove this issuse?
Thank you
Use completion
func getAllModels( completion: #escaping ([String] ,Bool) -> Void) {
var modelList:[String] = []
let url = BASE_URL + "getAllProductAndModelv2"
Alamofire.request(url, method:.get, parameters: [:], encoding: JSONEncoding.default).responseJSON { response in
let data = NSData(contentsOf: URL(string: url)!)
do {
if let data = data, let json = try JSONSerialization.jsonObject(with: data as Data) as? [String: Any], let models = json["models"] as? [[String:Any]] {
for model in models {
if let name = model["name"] as? String {
modelList.append(name)
}
}
completion(modelList,true)
}
}catch {
print("error")
completion([],false)
}
}
}
Then call it
self.getAllModels { (data, success) in
if(success)
{
// use data
}
}

Working with Alamofire

Working with Alamofire, getting:
Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error
Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character
2." error
Here is my code in NetworkClass:
class func requestPOSTURL(_ strURL : String, params :[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!)
success(resJson)
}
if responseObject.result.isFailure {
let error : Error = responseObject.result.error!
failure(error)
}
}
}
and request call:
let strURL = "myurl"
let parameters : [String: String] =
[
"user_name":"SNSH" as String,
"password":"SNOSH" as String,
"device_id":"0D4F5322-81C0-0000-9210-70DA0C6BC04C" as String,
]
if let json = try? JSONSerialization.data(withJSONObject: parameters, options: []) {
// here `json` is your JSON data, an array containing the String
// if you need a JSON string instead of data, then do this:
if let content = String(data: json, encoding: String.Encoding.utf8) {
// here `content` is the JSON data decoded as a String
print(content)
print(parameters)
NetworkClass.requestPOSTURL(strURL, params: parameters, success: {
(JSONResponse) -> Void in
print(JSONResponse)
}) {
(error) -> Void in
print(error)
}
}
}
I need to send String in parameters.
let urlAsString1 : String = API
let urlStr : NSString = urlAsString1.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
let params:NSMutableDictionary? = [
"user_name":"SNSH" as String,
"password":"SNOSH" as String,
"device_id":"0D4F5322-81C0-0000-9210-70DA0C6BC04C" as String,
];
let ulr = NSURL(string:urlStr as String)
let request = NSMutableURLRequest(URL: ulr!)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let data = try! NSJSONSerialization.dataWithJSONObject(params!, options: NSJSONWritingOptions.PrettyPrinted)
let json = NSString(data: data, encoding: NSUTF8StringEncoding)
if let json = json {
print(json)
}
request.HTTPBody = json!.dataUsingEncoding(NSUTF8StringEncoding);
Alamofire.request(request)
.responseJSON { response in
// do whatever you want here
switch (response.result) {
case .Success(let JSON):
print("JSON: \(JSON)")
let responseString = JSON as! NSDictionary
print(responseString)
break;
case .Failure:
break
}
}
}

Resources