Getting array instead of dictionary - json

I did't use third-party files for calling the API, here is the code:
func CallWebService(_ methodType: NSString, methodName: NSString, inputDict: NSDictionary, completion: #escaping (_ result: [String:AnyObject]) -> Void, failure:(_ failurMSG: NSString)->())
{
do {
let data: Data = try JSONSerialization.data(withJSONObject: inputDict, options: [])
//create request
let tmpString: String = "\(kBaseUrl)\(methodName)"
let urlString :String = tmpString.addingPercentEncoding( withAllowedCharacters: CharacterSet.urlQueryAllowed)!
let urlRequest = NSMutableURLRequest(url: URL(string: urlString)!)
urlRequest.httpMethod = methodType as String
urlRequest.httpBody = data
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
let task : URLSessionDataTask! = URLSession.shared.dataTask(with: urlRequest as URLRequest, completionHandler: {
(data, response, error) in
if let _ = error
{
DispatchQueue.main.async(execute: {
})
}else{
do {
let dict = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:AnyObject]
completion(dict)
}catch{
}
}
})
task.resume()
} catch {
DispatchQueue.main.async(execute: {
})
failure("Something goes wrong please try again.")
}
}
While I'm hitting API I got the below response:
["status": success, "category": {
"file_image" = "http://abcds.com/cphp/26/uploads/pdf1.png";
"file_name" = "1 \U0645\U0644\U0641 \U0627\U062e\U062a\U0628\U0627\U0631";
"file_path" = "http://hghg/images/2311201663231Michael20plat20du20jour20correctd.pdf";
id = 2;
"sub_name" = "P12 \U0641\U0631\U0639\U064a\U0629";
}]
This is not the correct format, what am I missing?
I need the output to be:
{"status": success, "category": [
"file_image" = "http://abcds.com/cphp/26/uploads/pdf1.png";
"file_name" = "1 \U0645\U0644\U0641 \U0627\U062e\U062a\U0628\U0627\U0631";
"file_path" = "http://hghg/images/2311201663231Michael20plat20du20jour20correctd.pdf";
id = 2;
"sub_name" = "P12 \U0641\U0631\U0639\U064a\U0629";
]
}

It looks to me like you ARE getting a dictionary.
EDIT:
In fact, the fact that your code is force-casting the JSON data to type [String:AnyObject] proves that the results are a dictionary. If they weren't, the force-cast line would crash:
let dict = try JSONSerialization.jsonObject(with: data!,
options: .allowFragments) as! [String:AnyObject]
(The as! [String:AnyObject] part of that line says "I am positive that the value can be cast to the type [String:AnyObject]. Please crash my program if not.)
Try adding this to your code:
print("dict type =" + String(describing:type(of:dict)))
I bet you get
dict type = Dictionary<String:AnyObject>

Related

Swift and JSON driving me crazy

I am really getting stuck on this.
I have created a JSON service, that returns data like this:
[
{
"docNameField": "Test",
"docNumField": 22832048,
"docVerField": 1,
"docDataBaseField": "Legal",
"docCheckedOutWhenField": "03/05/2020",
"whereCheckedOutField": "PC0X8J9RD"
}
]
This is Postman output.
No matter how I try, I cannot seem to be able to put together the correct combination og HTTP call, deserialization, types and so on to get a list of objects out in the end.
This func below outputs this:
JSON String: Optional("[{\"docNameField\":\"Test\",\"docNumField\":22832048,\"docVerField\":1,\"docDataBaseField\":\"Legal\",\"docCheckedOutWhenField\":\"03/05/2020\",\"whereCheckedOutField\":\"PC0X8J9RD\"}]")
func LoadLockedDocumentsByDocnum(docNum:Int32) {
let json: [String: Any] = ["action":"getCheckedOutDocuments","adminUserName":"\(APPuserName)","adminPassword":"\(APPuserPassword)","adminDomain":"\(APPuserDomain)","applicationKey":"19730905{testKey}","searchTerm":docNum]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
self.documentEntries.removeAll()
let url = URL(string: "https://{URL}//CheckOut")!
var request = URLRequest(url: url)
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") //Optional
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
let session = URLSession.shared
let dataTask = session.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if let resultat = response as! HTTPURLResponse?{
if resultat.statusCode == 200{
if error != nil {
}
else {
print(data!)
if let nydata = data{
print("JSON String: \(String(data: data!, encoding: .utf8))")
}
}
}}
}
dataTask.resume()
}
You seem to have come pretty close. To get a list of objects out, you first need to declare that object:
struct MyResponseObject: Decodable { // please give this a better name
let docNameField: String
let docNumField: Int
let docVerField: Int
let docDataBaseField: String
let docCheckedOutWhenField: Date
let whereCheckedOutField: String
}
And then use a JSONDecoder to deserialise the JSON. Instead of:
print("JSON String: \(String(data: data!, encoding: .utf8))")
write:
let decoder = JSONDecoder()
let formatter = DateFormatter()
formatter.dateFormat = "MM/dd/yyyy"
decoder.dateDecodingStrategy = .formatted(formatter)
do {
// here's your list of objects!
let listOfObjects = try decoder.decode([MyResponseObject].self, from: data!)
} catch let error {
print(error) // an error occurred, you can do something about it here
}

Load data from server using swift 4

I try to load the user profile like below
#IBAction func Btn_LoadDataFromDataBase(_ sender: UIButton) {
let myurl = "site.com/profile.php"
LoadURL(url: myurl)
}
func LoadURL(url: String) {
do{
let appURL = URL(string: url)! // convert string to URL
let data = try Data(contentsOf: appURL)
//error here on this line below :
let json1 = try JSONSerialization.jsonObject(with: data ) as! [String: Any]
print(json1)
let query1 = json1["profile"] as! [String: Any]
print(query1)
label_email.text = "Email : (query1["email"]!)"
}catch{
print("error in url")
}
}
if I test the json via webbrowser I get it like this:
{profile : [{"0":"999","id":"999","1":"1","email":"blabla#gmail.com","2":"1111","tel":"00122222222","3":"0" ..........
php code:
print "{profile : ".json_encode($user_profile,JSON_UNESCAPED_UNICODE)."}";
mysql_close($db);
?>
Please read the JSON carefully, there are only two different collection types
{} is dictionary ([String: Any])
[] is array ([Any] but in most cases [[String: Any]])
so the result for query1 (I changed the variable names to something more descriptive) is an array and you need a loop to print all elements:
let profileData = try JSONSerialization.jsonObject(with: data ) as! [String: Any]
let profiles = profileData["profile"] as! [[String: Any]] // could be even [[String:String]]
for profile in profiles {
print("Email :", profile["email"]!")
}
I'm wondering why so many owners of web services send the PHP arrays unnecessarily with both index and key.
And never load data from a remote URL synchronously, use asynchronous URLSession
You're better using URLRequest for async requests. You will need to pass your appURL as a parameter in a URLRequest and handle the answer in its completion handler.
An example:
let urlString = "https://swift.mrgott.pro/blog.json"
guard let url = URL(string: urlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!.localizedDescription)
}
guard let data = data else { return }
// Implement JSON decoding and parsing
do {
let articlesData = try JSONDecoder().decode([OBJECT YOU WANT].self, from: data)
} catch let jsonError {
print(jsonError)
}
}.resume()

Wait until JSON call is finished

I have this code in a part of my app:
let myUrl = NSURL(string: "http://aesolutions.ddns.net/data/load_tasks_calendar.php");
let request = NSMutableURLRequest(url: myUrl! as URL);
request.httpMethod = "POST";
let postString = "id_class=\(UserDefaults.standard.string(forKey: "userClass")!)";
request.httpBody = postString.data(using: String.Encoding.utf8);
MenuViewController.tasksCalendarArray.removeAll()
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
if error != nil{
print("error1=\(String(describing: error))")
return
}
var _: NSError?
do{
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSArray
if let parseJSON: NSArray = json {
for index in 0...parseJSON.count-1 {
if (parseJSON[index] is NSNull){
MenuViewController.tasksCalendarArray.removeAll()
}else{
let compito = parseJSON[index] as! [String:Any]
let task = tasks.init(taskId: compito["id"] as! String,taskType: compito["type"] as! String,taskSubject: compito["subject"] as! String,taskDate: compito["date"] as! String,taskComment: compito["comment"] as! String)
MenuViewController.tasksCalendarArray.append(task)
}
}
}
}catch{
print("error2=\(error)")
return
}
DispatchQueue.main.async(execute: {
self.performSegue(withIdentifier: "loginToDiary", sender: self)
});
}
task.resume();
I want to perform the segue only when I load all the array. How can I wait until the json is terminated or is done correctly? Because I noticed that sometimes it is correct and other times it performs the segue and so the array is empty and then in the app there are errors.
Can someone help me also adding an alert with a kind of "loading message" to wait that the array is loading?

Not getting json response on first click using alamofire

I am using alamofire for getting JSON response.
When I click on the button for the first time, I am not getting response. I've checked after few times just to be sure that whether my internet speed is low. Internet speed is okay and still every time this happens, not entering in the if condition to print the response. Please help. Thanks in advance.!!
Below is my code
Alamofire.request(url).responseJSON { response in
if let JSON = response.result.value
{
let responseRes = JSON as? Dictionary<String,AnyObject>
print("Response = \(responseRes!)")
}
}
This will perfectly work in Swift 3.1
func testURL () {
let parameter = ["id": 19, "name": "", "image_name": "", "largeimage": "", "catdata": ["category_name"]] as [String: Any]
//Here parameter as per your web service.
//var parameter = [String : Any]()
//print("t:-\(parameter)")
guard let url = URL(string: "YourWebServiceURL") else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameter, options: []) else { return }
request.httpBody = httpBody
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let session = URLSession.shared
session.dataTask(with: request) {(data:Data?, response:URLResponse?, error:Error?) in
if let response = response {
print(response)
do {
let json = try JSONSerialization.jsonObject(with: data!) as! [String: Any]
print(json["data"]!)
let dataarray = json["data"]! as! Array<Any>
for i in dataarray {
let webServiceArray = i as! [String : Any]
//Below all the Object as per you webService objects.
print(webServiceArray["name"]!)
print(webServiceArray["largeimage"]!)
print(webServiceArray["image_name"]!)
print(webServiceArray["id"]!)
}
} catch {
print("Error deserializing JSON: \(error)")
}
}
}
.resume()
}
Access this function in ViewDidLoad.

how to retrive JSON data by using swift

I am retrieving datas from JSON by using swift. I am new to JSON. I don't how to retrieve this nested values. My previous question is Issue raised, while retriving datas from JSON by using swift . I got clarified. This is new to me. My JSON format is below. Kindly guide me.
JSON Response formats:
{
"salutation": {
"id": 1,
"salutation": "Mr"
},
"firstName": "AAA",
"middleName": "BBB",
"lastName": "C",
"employeeId": "RD484",
"station": {
"id": 86,
"stationCode": null,
"stationName": "DDD",
"subDivision": null,
"address": null
},
"subDivsion": {
"id": 11,
"divisionCode": "11",
"divisionDesc": "EEE",
"division": null
}
}
//My Attempt:
var maindict = NSDictionary() //Global declaration
var session = NSURLSession.sharedSession()
//now create the NSMutableRequest object using the url object
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST" //set http method as POST
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: &err) // pass dictionary to nsdata object and set it as request body
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
println("Response: \(response)")
self.maindict = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as [String: AnyObject]
var err: NSError?
var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSDictionary
// Did the JSONObjectWithData constructor return an error? If so, log the error to the console
if(err != nil) {
println(err!.localizedDescription)
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
//println("Error could not parse JSON: '\(jsonStr)'")
}
else {
// The JSONObjectWithData constructor didn't return an error. But, we should still
// check and make sure that json has a value using optional binding.
if let parseJSON = json {
// Okay, the parsedJSON is here, let's get the value for 'success' out of it
var success = parseJSON["success"] as? Int
println("Succes: \(success)")
self.dataFromJSON() //METHOD CALLING
}
else {
// Woa, okay the json object was nil, something went worng. Maybe the server isn't running?
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Error could not parse JSON: \(jsonStr)")
//println("AUTHENTICATION FAILED")
}
}
})
task.resume()
func dataFromJSON()
{
println("Main Dict Values: \(maindict)") //PRINTING ALL VALUES
let dataArray = maindict["firstName"] as? [String:AnyObject]
println("FirstName Values: \(dataArray)") // PRINTING NIL VALUES
}
Your data structure does not begin with an array this time, but with a Dictionary. Your structure is like is:
root Dictionary -> "salutation" -> Dictionary
root Dictionary -> "station" -> Dictionary
root Dictionary -> "subDivsion" -> Dictionary
Let's say you want to access the "id" of "salutation", then:
// Just an exemple of how to download, surely you have your own way
func getJSON(url: NSURL) {
let session = NSURLSession.sharedSession()
let request = NSURLRequest(URL: url)
let task = session.dataTaskWithRequest(request){
(data, response, downloadError) -> Void in
if let error = downloadError {
println(error.localizedDescription)
} else {
var jsonError: NSError?
// cast the result as a Dictionary
if let dict = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &jsonError) as? [String: AnyObject] {
// print the dictionary to check contents
println(dict)
if let salutationDictionary = dict["salutation"] as? [String: AnyObject] {
if let id = salutationDictionary["id"] as? Int {
println(id)
}
}
}
if jsonError != nil {
println(jsonError)
}
}
}
task.resume()
}
EDIT:
My friend, your code was a mess... I suggest to you to do some cleaning when you've got errors, it helps to debug. Anyway, here's a corrected version of your new code. Pay attention to how maindict is declared on the first line. Also, you had one unnecessary call to NSJSONSerialization, I simplified it. Note: for the sake of the example, I've included your dataFromJSON function code directly inside if let parseJSON ..., but of course it doesn't mean you have to do the same.
var maindict: [String: AnyObject]?
var session = NSURLSession.sharedSession()
//let parameters = ...
let request = NSMutableURLRequest(URL: your_url!)
request.HTTPMethod = "POST"
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: &err)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
println("Response: \(response)")
var err: NSError?
maindict = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &err) as? [String: AnyObject]
if err != nil {
println(err!.localizedDescription)
} else {
if let parseJSON = maindict {
var success = parseJSON["success"] as? Int
println("Succes: \(success)")
println("Main Dict Values: \(maindict)")
let firstName = maindict!["firstName"] as? String
println("FirstName: \(firstName)")
}
else {
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Error could not parse JSON: \(jsonStr)")
}
}
})
task.resume()
Please pay attention to the details and study my modifications by comparing with your attempt. My answer has been tested on my own server and it works, so you can use it as a working base.
The easiest way to do this is to use a library.
1) You can use swiftyJSON. It uses the objective C JSON parsing library.
https://github.com/SwiftyJSON/SwiftyJSON
2) If you want a library which uses a pure swift parser try JSONSwift. The readme on github shows how you can retrieve nested values from the JSON file. And integrating it in your project just requires you to import a single file. https://github.com/geekskool/JSONSwift