Json with Swift 2, extracting data - json

I'm having this issue where I can't access values from JSON response,
the response is : {"result":[true]}
and when the JSON gets it with this code
do{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)
let result:String = json["result"]
print(result)
}catch {
print("Error with Json: \(error)")
}
I get an error, and the when I did the debug, I saw that json had the following
how json is stored
is there anyway to access the result from json ? it didn't work treating it as an array nor as dictionary
any ideas ?
thanks

result is not String, it's an Array of Bool (represented by the brackets).
Basically do not annotate types unless the compiler needs them.
Cast the JSON to the proper type and use Swift native collection types. It's also recommended to use optional bindings to avoid unexpected crashes.
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments) as? [String:AnyObject],
result = json["result"] as? [Bool] where !result.isEmpty {
print(result[0])
}
} catch {
print("Error with Json: \(error)")
}

Try like this
do{
let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments) as! NSDictionary
let result = json["result"] as! NSArray
print(result)
let boole = result[0];
}catch {
print("Error with Json: \(error)")
}

Related

Swift MacOS getting bad json response from jsonSerialization

I am trying to convert a string to JSON in Swift.
Here's the string, which I am getting by pulling the innerHTML from a WKWebView.
{"list":{"pagination":{"count":3,"hasMoreItems":false,"totalItems":3,"skipCount":0,"maxItems":100},"entries":[{"entry":{"createdAt":"2020-06-16T21:00:32.714+0000","isFolder":false,"isFile":true,"createdByUser":{"id":"UserFName.userLName#xxxxxxxx.com","displayName":"UserFName userLName"},"modifiedAt":"2020-06-16T21:00:32.714+0000","modifiedByUser":{"id":"UserFName.userLName#xxxxxxxx.com","displayName":"UserFName userLName"},"naxxxxxxxxme":"00-invest-2020-06-16-17-00-32-716.txt","id":"028b4c82-09b8-4ee5-b4fa-9696a33b026d","nodeType":"log:fileNode","content":{"mimeType":"text/plain","mimeTypeName":"Plain Text","sizeInBytes":609373,"encoding":"UTF-8"},"parentId":"ba647bfc-a889-4d91-9211-4220cfe7d90a"}},{"entry":{"createdAt":"2020-06-16T21:01:12.828+0000","isFolder":false,"isFile":true,"createdByUser":{"id":"UserFName.userLName#xxxxxxxx.com","displayName":"UserFName userLName"},"modifiedAt":"2020-06-16T21:01:12.828+0000","modifiedByUser":{"id":"UserFName.userLName#xxxxxxxx.com","displayName":"UserFName userLName"},"name":"00-monetize-2020-06-16-17-01-12-830.txt","id":"d6412e3a-fea5-4d4d-a962-d91cde294bc9","nodeType":"log:fileNode","content":{"mimeType":"text/plain","mimeTypeName":"Plain Text","sizeInBytes":996653,"encoding":"UTF-8"},"parentId":"ba647bfc-a889-4d91-9211-4220cfe7d90a"}},{"entry":{"createdAt":"2020-06-16T18:33:49.344+0000","isFolder":true,"isFile":false,"createdByUser":{"id":"UserFName.userLName#xxxxxxxx.com","displayName":"UserFName userLName"},"modifiedAt":"2020-06-16T18:34:49.211+0000","modifiedByUser":{"id":"UserFName.userLName#xxxxxxxx.com","displayName":"UserFName userLName"},"name":"20200616","id":"d881db96-ddcb-44ae-99e1-ffe3ac0c2810","nodeType":"cm:folder","parentId":"2fcf4c49-be4c-4f2c-a90b-654ae092c63e"}}]}}
I've checked the string in JSON Lint and it says it is valid.
Here's what I am doing in my code to convert it:
let strJSONLiteral = """
\(strJSON)
"""
//convert string to json
let data = strJSONLiteral.data(using: .utf8)!
do {
if let myJSON = try JSONSerialization.jsonObject(with: data, options : .allowFragments) as? [Dictionary<String,Any>]
{
print(myJSON) // use the json here
} else {
print("bad json")
}
} catch let error as NSError {
print(error.localizedDescription)
}
The error is occurring in the JSONSerialization attempt. I'm getting nil for myJSON. Data check looked ok, has 1600+ bytes.
The top-level JSON is of type Dictionary while you're trying to decode an Array of Dictionary. To fix this
Replace:
if let myJSON = try JSONSerialization.jsonObject(with: data, options : .allowFragments) as? [Dictionary<String,Any>]
With:
if let myJSON = try JSONSerialization.jsonObject(with: data, options : .allowFragments) as? Dictionary<String,Any>
Or you can just use [String: Any].
Add-on: You should probably do a bit of research on Codable and try to use it for these scenarios.

Cannot convert Data to NSDictionary

I'm trying to convert data from URLSession to a NSDictionary but it fails when converting the data to dictionary.
Following:
let json = try? JSONSerialization.jsonObject(with: data!, options: [])
print(json ?? "NotWorking")
outputs
(
{
babyId = 1;
id = 17;
timestamp = "2018-06-30 09:23:27";
}
)
But when I try to convert it into a Dictionary with following it outputs nil.
let json = try? JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
The webpage outputs
[{"id":"17","babyId":"1","timestamp":"2018-06-30 09:23:27"}]
Where does the error occurs?
[ ] means array in JSON. { } means dictionary. You have an array of dictionary. Note that when you print an array in Swift, you will see ( ).
Don't use NSArray or NSDictionary in Swift without a very clearly understood and specific reason. Use a Swift array and dictionary of the proper types.
Your code should be:
do {
if let results = try JSONSerialization.jsonObject(with: data!) as? [[String:Any]] {
// results is now an array of dictionary, access what you need
} else {
print("JSON was not the expected array of dictonary")
}
} catch {
print("Can't process JSON: \(error)")
}
And really you shouldn't be using data! either. Somewhere above this you should have a if let data = data {

How to guarantee valid JSON in Swift 4?

I'm trying to work with JSON data returned from a service. The JSON is, according to JSON validators, valid and is very simple:
[{"ID":"SDS-T589863","TotalRisk":0.2458,"TotalScore":641.032}]
However trying to parse it in my Swift 4 code it is mysteriously (to me at least) invalid. Here's my attempt to parse it:
// make the request
let task = session.dataTask(with: urlRequest) {
(data, response, error) in
// check for any errors
guard error == nil else {
print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
// this is fine:
guard let ddd = String(bytes: responseData, encoding: String.Encoding.utf8) else {
print("can't")
return
}
print(ddd) // prints [{"ID":"SDS-T589863","TotalRisk":0.2458,"TotalScore":641.032}] happily
do {
// cannot serialize
guard let risk = try JSONSerialization.jsonObject(with: responseData, options: [JSONSerialization.ReadingOptions.allowFragments])
as? [String: Any]
else {
print("error trying to convert data to JSON")
return
}
print(risk)
} catch {
print("error trying to convert data to JSON")
return
}
}
task.resume()
}
Assuming that I have no control over the JSON object or the format in which it is returned to me, is there a way to tell what is wrong with the JSON and perhaps format the response so that it can be serialized correctly?
You should cast your data to the [[String: Any]] type because you have array in response.
You are trying to cast to [String: Any], but you have an array of [String: Any] because your response enclosed in [] brackets.
Example:
let risk = try JSONSerialization.jsonObject(with: responseData, options: [JSONSerialization.ReadingOptions.allowFragments]) as? [[String: Any]]
Or if you want to get just only one [String: Any] object from response you can write:
let risk = (try JSONSerialization.jsonObject(with: responseData, options: [JSONSerialization.ReadingOptions.allowFragments]) as? [[String: Any]])?.first
Or if your object can be an array or not an array (but it sounds a little bit strange) you could try to cast to several possible types.
The response type is array of json objects so you have to cast it to [[String: Any]]. Since you are using Swift 4, you can use Decodable type which maps the model to the response.
let task = URLSession().dataTask(with: urlRequest) { (data, response, error) in
// check for any errors
guard error == nil else {
print(error!)
return
}
// make sure we got data
guard let responseData = data else {
print("Error: did not receive data")
return
}
do {
let decoder = JSONDecoder()
let riskArray = try decoder.decode([Risk].self, from: responseData)
print(riskArray)
} catch {
print("error trying to convert data to Model")
print(error.localizedDescription)
}
}
task.resume()
You can define your Model struct like
struct Risk: Decodable {
let id: String
let totalRisk: Double
let totalScore: Double
enum CodingKeys: String, CodingKey {
case id = "ID"
case totalRisk = "TotalRisk"
case totalScore = "TotalScore"
}
}
You can read more about Decodable protocol here

Issue with JSON and Swift - \u00c3\u00a9 é instead of é

I am trying to display some JSON data on my iOS app but I am having an issue with displaying it correctly using Swift.
When I use the normal JSONSerializer for \u00c3\u00a9 i get é but I want to display é. I don't understand if it is an issue with say using UTF-16 rather than UTF-8 or something else?
Does anyone have any suggestions how I would convert \u00c3\u00a9 straight to é in Swift, from a JSON received from an API.
Not sure which encoding you're using, but this code works for both .utf8 and .utf16:
let jsonString = "{\"foo\": \"áéíóú\"}"
let data = jsonString.data(using: .utf16)!
do {
let object = try JSONSerialization.jsonObject(with: data, options: [])
if let dict = object as? [AnyHashable: Any], let text = dict["foo"] as? String {
print("Extracted text: \(text)")
}
}
catch let e {
// TODO: Handle error
print("Error processing JSON: \(e)")
}
was the same problem. Necessary use this code, it works:
Alamofire.request(url, method: .get, parameters: params)
.responseJSON{ response in
guard let data = response.data else {return}
do {
let jsonResult = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! NSDictionary
let json = jsonResult["response_goods"] as! [[String:String]]
print(json) } catch let err as NSError {print(err)
}
}

JSON parsing using Swift

I have a php file which create a JSON array. This the JSON output.
[{"employee_id":"1","employee_name":"Steve","employee_designation":"VP","employee_salary":"60000"},{"employee_id":"2","employee_name":"Robert","employee_designation":"Executive","employee_salary":"20000"},{"employee_id":"3","employee_name":"Luci","employee_designation":"Manager","employee_salary":"40000"},{"employee_id":"4","employee_name":"Joe","employee_designation":"Executive","employee_salary":"25000"},{"employee_id":"5","employee_name":"Julia","employee_designation":"Trainee","employee_salary":"10000"}]
I want to parse this array using swift in my app. So I used the following code to parse the JSON array
func jsonParser() {
let urlPath = "xxxxxxxxx/dbretrieve.php"
guard let endpoint = NSURL(string: urlPath) else { print("Error creating endpoint");return }
let request = NSMutableURLRequest(URL:endpoint)
NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
do {
guard let dat = data else { throw JSONError.NoData }
guard let json = try NSJSONSerialization.JSONObjectWithData(dat, options:.AllowFragments) as? NSArray else { throw JSONError.ConversionFailed }
print(json)
} catch let error as JSONError {
print(error.rawValue)
} catch {
print(error)
}
}.resume()
}
but I get the following error
Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}
What's the mistake I made?
I checked your json array. Its perfect.
The point where the issue might be serialization.
Depends on data you get in response
Try to disable debugging mode in PHP
Try below serialisation code
Sample Code:
do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as! [String: AnyObject]
print(json)
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}