My swift app fail to convert NSData to Json - ios

I was trying to convert NSData to Json by doing this:
let jdata = getJSON("https://api.myjson.com/bins/16j2i")
do {
let json = try NSJSONSerialization.JSONObjectWithData(jdata, options: []) as! [String: AnyObject]
print(json)
} catch {
print("\(error)")
}
This is the getJSON method
func getJSON(url:String) -> NSData {
return NSData(contentsOfURL: NSURL(string: url)!)!
}
A error says that could not cast value of type '_NSCFArray' to 'NSDictionary'. Any ideas? Please

The root element of your JSON is array not dictionary (Your format looks something like [{...},{...}] ). For fixing this error you need to change the parsing code to:
let json = try NSJSONSerialization.JSONObjectWithData(jdata, options: []) as! [[String: AnyObject]]

let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
let convert_array = responseString?.description
// Convert server json response to NSDictionary
do {
if let convertedJsonIntoDict = try JSONSerialization.jsonObject(with: data!, options: []) as? NSArray {
print(convertedJsonIntoDict)
}

let data = text.data(using: String.Encoding.utf8)
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:Any]
Working Fine for Swift 3.1 & Swift 4

Related

How to convert rawString to JSON Array?

I am trying to convert jsonString.rawString() to JSON object, but it returns nil.
let jsonData = newsfeedData.rawString()?.data(using: .utf8)
let object = try? JSONSerialization.jsonObject(with: jsonData!, options: .mutableContainers)
print(object) //it returns nil
This is the jsonString:
Printing description of newsfeedData.rawString:
"Data:[{\"UserID\":1,\"post\":[{\"PostId\":1,\"UserId\":1,\"UserName\":\"party Patel\",\"ImagePath\":\"/Files/User/user_20180606_040913967_Hydrangeas.jpg\",\"FileId\":2108,\"FileName\":\"Business Category.png\",\"Email\":\"parth.patel#shaligraminfotech.com\",\"Location\":\"Ahmedabad\",\"PostDescription\":\"P7800\",\"PostPath\":\"/Files/NewsFeed/Photo/fd1c7fcf-e1d0-4ffe-afb4-42bb3fea4fa4.PNG\",\"UserRole\":\"Business\",\"BusinessName\":\"Shaligram infotech\",\"PostType\":false,\"Type\":\"Photo\",\"AdminUserId\":0,\"DisplayText\":null,\"PostCreatedDate\":\"06/08/2018\",\"LikeCount\":4,\"CommentCount\":4,\"ViewCount\":3},{\"PostId\":3,\"UserId\":1,\"UserName\":\"party Patel\",\"ImagePath\":\"/Files/User/user_20180606_040913967_Hydrangeas.jpg\",\"FileId\":2110,\"FileName\":\"DiscovrUS.mov\",\"Email\":\"parth.patel#shaligraminfotech.com\",\"Location\":\"Ahmedabad\",\"PostDescription\":\"P7800\",\"PostPath\":\"/Files/NewsFeed/Video/ab48c228-d7b5-4dff-af56-31a0ac159a35.MOV\",\"UserRole\":\"Business\",\"BusinessName\":\"Shaligram infotech\",\"PostType\":true,\"Type\":\"Video\",\"AdminUserId\":0,\"DisplayText\":null,\"PostCreatedDate\":\"06/08/2018\",\"LikeCount\":1,\"CommentCount\":0,\"ViewCount\":1}]},{\"UserID\":2,\"post\":[{\"PostId\":2,\"UserId\":2,\"UserName\":\"tejas Padia\",\"ImagePath\":\"/Files/User/user_20180606_062946997_Tulips.jpg\",\"FileId\":2109,\"FileName\":\"DiscovrUS.mov\",\"Email\":\"tejaspadia#gmail.com\",\"Location\":\"India\",\"PostDescription\":\"P7800\",\"PostPath\":\"/Files/NewsFeed/Video/2d892eaf-b6dc-433d-985b-2a4588ffd307.MOV\",\"UserRole\":\"Individual\",\"BusinessName\":\"\",\"PostType\":true,\"Type\":\"Video\",\"AdminUserId\":0,\"DisplayText\":null,\"PostCreatedDate\":\"06/08/2018\",\"LikeCount\":3,\"CommentCount\":0,\"ViewCount\":0}]}]"
Make sure json format your value invalid format
just try replace Data: to empty string "" , Your code it's working
do{
var json = "Data:[{\"UserID\":1,\"post\":[{\"PostId\":1,\"UserId\":1,\"UserName\":\"party Patel\",\"ImagePath\":\"/Files/User/user_20180606_040913967_Hydrangeas.jpg\",\"FileId\":2108,\"FileName\":\"Business Category.png\",\"Email\":\"parth.patel#shaligraminfotech.com\",\"Location\":\"Ahmedabad\",\"PostDescription\":\"P7800\",\"PostPath\":\"/Files/NewsFeed/Photo/fd1c7fcf-e1d0-4ffe-afb4-42bb3fea4fa4.PNG\",\"UserRole\":\"Business\",\"BusinessName\":\"Shaligram infotech\",\"PostType\":false,\"Type\":\"Photo\",\"AdminUserId\":0,\"DisplayText\":null,\"PostCreatedDate\":\"06/08/2018\",\"LikeCount\":4,\"CommentCount\":4,\"ViewCount\":3},{\"PostId\":3,\"UserId\":1,\"UserName\":\"party Patel\",\"ImagePath\":\"/Files/User/user_20180606_040913967_Hydrangeas.jpg\",\"FileId\":2110,\"FileName\":\"DiscovrUS.mov\",\"Email\":\"parth.patel#shaligraminfotech.com\",\"Location\":\"Ahmedabad\",\"PostDescription\":\"P7800\",\"PostPath\":\"/Files/NewsFeed/Video/ab48c228-d7b5-4dff-af56-31a0ac159a35.MOV\",\"UserRole\":\"Business\",\"BusinessName\":\"Shaligram infotech\",\"PostType\":true,\"Type\":\"Video\",\"AdminUserId\":0,\"DisplayText\":null,\"PostCreatedDate\":\"06/08/2018\",\"LikeCount\":1,\"CommentCount\":0,\"ViewCount\":1}]},{\"UserID\":2,\"post\":[{\"PostId\":2,\"UserId\":2,\"UserName\":\"tejas Padia\",\"ImagePath\":\"/Files/User/user_20180606_062946997_Tulips.jpg\",\"FileId\":2109,\"FileName\":\"DiscovrUS.mov\",\"Email\":\"tejaspadia#gmail.com\",\"Location\":\"India\",\"PostDescription\":\"P7800\",\"PostPath\":\"/Files/NewsFeed/Video/2d892eaf-b6dc-433d-985b-2a4588ffd307.MOV\",\"UserRole\":\"Individual\",\"BusinessName\":\"\",\"PostType\":true,\"Type\":\"Video\",\"AdminUserId\":0,\"DisplayText\":null,\"PostCreatedDate\":\"06/08/2018\",\"LikeCount\":3,\"CommentCount\":0,\"ViewCount\":0}]}]"
json = json.replacingOccurrences(of: "Data:", with: "")
if let data = json.data(using: .utf8) {
do {
let jsonResult = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]]
print(jsonResult)
} catch {
print(error.localizedDescription)
}
}

Deserialize JSON from String to object

I try to deseralize my JSON that look like this :
{
"access_token": "This_is_my_token"
"item01": "blabla"
"item02": "blabla"
...
}
and I want to save only access_token into a variable. Not a big deal.
For exemple, with PHP it is something really simple like:
<?php
$jsonObj = json_decode($jsonString);
$access_token = $jsonObj["access_token"];
?>
But it doesn't look this easy with Swift. I try many things but nothing works for me.
Here is the code I have:
dataString = String(data: data, encoding: .utf8)!
do {
let anyObj: AnyObject? = try JSONSerialization.jsonObject(with: dataString, options: JSONSerialization.ReadingOptions([])) as AnyObject
guard let access_token = anyObj?.firstItem as? [String: AnyObject]
else {return}
print(access_token)
} catch {
print("JSON Deserial. Error")
}
with this code, I can't manipulate anyObj like I do with $jsonObj in PHP, there is no way to do something like anyObj["access_token"]
How can I access to this item in the object ?
Thanks
You can simply call this function by passing your data here and it will return a dictionary. From that dictionary you can pick access_token value.
class func parseJsonToDictionary(data: Data) -> ([String:AnyObject]?,Error?) {
let _ = NSString(data: data, encoding:String.Encoding.utf8.rawValue)
do {
let JSONObject = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
return (JSONObject as? [String:AnyObject],nil)
} catch let error as NSError {
return (nil,error)
}
}
If you are having json string and not json data then you can use this method to convert it to dictionary.
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
}
let str = "{\"name\":\"James\"}"
let dict = convertToDictionary(text: str)
Later you can get value of access_token from dictionary.
Instead of casting anyObj to AnyObject try casting it to a dictionary of strings like
let anyObj: AnyObject? = try JSONSerialization.jsonObject(with: dataString, options: JSONSerialization.ReadingOptions([])) as [String:String]
Then you can get accessToken by using anyObj["access_token"]

xcode swift 3.0 parse JSON response

I'm trying to get the status code and put it in an if statement. This code will run in the xCode playground.
Right now status returns as ["200"] with square brackets around it. If I remove the brackets it returns as nil.
How to I return status as 200 and put it in an if statement?
import Foundation
let str = "{\"names\": [\"Bob\", \"Tim\", \"Tina\"],\"status\"[\"200\"],\"message\":\"User has been created\",\"id\":null,\"username\":\"asdf\"}"//"{\"names\": [\"Bob\", \"Tim\", \"Tina\"]}"
let data = str.data(using: String.Encoding.utf8, allowLossyConversion: false)!
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String: AnyObject]
let status = json["status"] as? [String]
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
You can try this workaround:
let responseString = Optional("{\"status\":\"200\",\"message\":\"User has been created\",\"id\":null,\"username\":\"asdf\"}")
let responseData = responseString?.data(using: .utf8)
let dict: Dictionary<String, Any> = try JSONSerialization.jsonObject(with: responseData!, options: []) as! Dictionary<String, Any>
let status = dict["status"] // optional value stored with "status" key in Dictionary
It seems that actually, Swift 3 refuses to compile using:
let dict: Dictionary<String, Any> = try JSONSerialization.jsonObject(with: responseString?.data(using: .utf8)!, options: []) as! Dictionary<String, Any>
Swift 3 claims that responseString?.data(using: .utf8)! should be unwrapped although it is already.
let str = "{\"names\": [\"Bob\", \"Tim\", \"Tina\"],\"status\":[\"200\"],\"message\":\"User has been created\",\"id\":null,\"username\":\"asdf\"}"//"{\"names\": [\"Bob\", \"Tim\", \"Tina\"]}"
let data = str.data(using: .utf8)
do {
let json = try JSONSerialization.jsonObject(with: data!, options: []) as! [String: AnyObject]
let status = json["status"] as? [String]
let yourValue = status?[0]
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
I just add ( : ) after status in json formate to be invalid

Swift iOS NSJsonSerialization fails silently

I have the following code fetching json data via an API.
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
// print("Response: \(response)")
// print("DATA: \(data)")
if data != nil {
do {
print( NSString(data: data!, encoding: NSUTF8StringEncoding))
if let jsonResults = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSArray {
print("\n\nThe return JSON looks like this: \(jsonResults)\n\n")
}
} catch {
print("\n\nProblem getting JSON back from GET API.\n\n")
}
} else {
print("API Not Available") // data was nil
}
}) // task
task.resume()
The "print" of the NSString shows what to my eyes looks like valid JSON. The console shows this as:
Optional({"id":15,"user_id":11,"breed_id":593,"gender":"Male","age_years":5,"tally":{"count":1246,"struvite":716,"calcium_oxalate":388,"urate":217,"calcium_phosphate":30,"silica":21,"compound":41},"created_at":"2016-02-04T08:26:14.719-06:00","updated_at":"2016-02-04T08:26:14.719-06:00"})
However, the "if let jsonResults =" statement does execute successfully (there is no jsonResults printed) and the catch does not execute either -- so it seems like a silent error.
A very similar call works on another part of the API. The only big difference is that "tally" in the JSON return on this call is nested.
Any suggestions are appreciated.
The JSON may not be serialised properly, so try to replace:
if let jsonResults = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSArray
with:
if let jsonResults = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! [String: AnyObject]
let jsonResults = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSMutableDictionary
This implementation is a lot neater, and you don't have to specify their data types.
The answer was a combination of dzk and ishaq's responses.
I replaced
if let jsonResults = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSArray
with:
if let jsonResults = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
The essential piece was casting it as a Dictionary, not an Array.

Swift 2 parse Json as Optional to array

I am getting list of countries from a web service. After receiving it I used this code to process it:
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
// triggering callback function that should be processed in the call
// doing logic
} else {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? AnyObject {
completion(json)
} else {
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Error could not parse JSON string: \(jsonStr)")
}
}
And after that list looks like this (it ends up in this part NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? AnyObject) :
Optional((
{
"country_code" = AF;
"dial_code" = 93;
id = 1;
name = Afghanistan;
},
{
"country_code" = DZ;
"dial_code" = 213;
id = 3;
name = Algeria;
},
{
"country_code" = AD;
"dial_code" = 376;
id = 4;
name = Andorra;
}
))
I should now convert this json object to array (or NSDictionary somehow) and to loop through it. Can someone advice how?
Currently you can't loop through your object because it has been cast as AnyObject by your code. With your current code you're casting the JSON data either as? NSDictionary or as? AnyObject.
But since JSON always start with a dictionary or an array, you should do this instead (keeping your example):
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
// process "json" as a dictionary
} else if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? NSArray {
// process "json" as an array
} else {
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Error could not parse JSON string: \(jsonStr)")
}
And ideally you would use Swift dictionaries and arrays instead of Foundation's NSDictionary and NSArray, but that is up to you.
try this
let jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers) as? [String:AnyObject]

Resources