As a Swift newcomer, I am very confused about how to parse some JSON data obtained from an API. I am able to get the JSON data from the api using an alamofire request. At this point, I think I have an NSDictionary object, JSON as print(JSON) logs to console a good deal of JSON.
if let result = response.result.value {
let JSON = result as! NSDictionary
print("this is what JSON is")
print(JSON)
My question is, first, is JSON in fact an NSDictionary. Second, how would I access a value in the JSON. Do I need to first convert this to a data object. Or how do I get at the nested data.
For example, let's say the JSON looks like this:
{
"contact": {
"first": "Bob",
"second":"Jones"
}
}
I came across this code on SO:
let data = JSON(data: JSON)
print("data\(data["contact"]["first"])")
But it throws an error. I have swiftyJSON installed but happy for solution with or without it.
Thanks in advance for any suggestions
Can you try
if let result = response.result.value as? [String:Any] {
if let contact = result["contact"] as? [String:Any] {
if let first = contact["first"] as? String {
print(first)
}
}
}
also this
let data = JSON(data: JSON)
gives error because parameter should be of type Data not Dictionary
I would prefer to return Data from Alamofire request and use Decodable to parse it and convert to required model
TRY THIS!
if let data = response.data {
let jsonData = JSON(data: data)
print("data : \(jsonData["contact"]["first"].string)")
}
Swift4 introduce amazing Codable protocol.
Using Codable, we can model JSONObject or PropertyList file into equivalent Struct or Classes by writing very few lines of code.
There are many online tool available which creates model class from you JSON
(http://www.json4swift.com/)
Example
let decoder = JSONDecoder()
let parsedObject = try decoder.decode(Class.self, from: data)
You can find detail at below link:
https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types
Related
I have some objects which have Codable protocol so they coming from service API and turn into dictionaries. There are lots of Service function in the project so I can not fetch the the point where they turn into JSON or dictionary and I can not the see first state of these objects.
So is there any way so that i can see that dictionary values in console or somewhere else even if they had already become JSON ?
Thanks in advance.
You can use this extension to convert it from dictionary to JSON and by using it you can print it in the console.
extension Dictionary {
var json: String {
let invalidJson = "Not a valid JSON"
do {
let jsonData = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
return String(bytes: jsonData, encoding: String.Encoding.utf8) ?? invalidJson
} catch {
return invalidJson
}
}
func printJson() {
print(json)
}
}
suppose you have yourDicObj decoded from JSON and you want to see it as JSON before decoded on the console
usage in console :
lldb po yourDicObj.printJson
it will print as JSON
I am new to the swift language. I am trying to fetching some data from an API call and response to that API is "{"status":1,"msg":"Please enter Mobile number"}"
I have the above response in a string How can I convert this string to JSONObject and then parse it and get status value?
I am an android developer we have evaluate expression while debugging code is there some like in Xcode so I can execute some code run time
I suggest use SwiftyJson library .
first create a model from your json using http://jsoncafe.com
you can parse data and model it using the following code
let json = JSON(response)
let message = Message(fromJson: json )
then you have access to your variable => message.status
JSONSerialization is the tool you are looking for. It handles converting JSON into objects, and objects into JSON. If you receive a JSON string represented as Data, you can pass it into JSONSerialization.jsonObject(with:options:)
If you are wanting to convert Data into an object:
//Where 'data' is the data received from the API call
guard let jsonObject = try? (JSONSerialization.jsonObject(with: data, options: []) as! [String:Any]) else {
return
}
let status = jsonObject["status"] as! Int
let message = jsonObject["msg"] as! String
If you are wanting to convert String into an object:
//Where 'string' is the JSON as a String
guard let jsonObject = try? (JSONSerialization.jsonObject(with: string.data(using: .utf8), options: []) as! [String:Any]) else {
return
}
let status = jsonObject["status"] as! Int
let message = jsonObject["msg"] as! String
Source: https://developer.apple.com/documentation/foundation/jsonserialization
Hey guys so I create a NSMutableArray send it to my mysql server convert it to base64, then when the app reads the data, it decodes the base64 code into a string format. Now im trying to convert the string back into an NSMutableArray. I cant seem to get it work heres the code that converts it to a string.
let string = String(data: (Data(base64Encoded:((data?.value(forKey: "14112017") as! NSArray)[0] as! NSDictionary)["data"] as! String)!), encoding: .utf8)!
So the answer to my question ended up converting my array to a JSONstring first which I did by using:
let jsonData: Data? = try? JSONSerialization.data(withJSONObject: UniversalArray)
let jsonString = String(data: jsonData!, encoding: .utf8)
then when retreiving it I get back my jsonString, however it has "\" which I replace using:
let cleanJsonString = myData.replacingOccurrences(of: "\\", with: "")
then to finally finish it off I just send this cleanJsonString to this function:
func convertToDictionary(text: String) -> Any? {
if let data = text.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: [])
} catch {
print(error.localizedDescription)
}
}
return nil
}
when calling this function I used this:
let array = convertToDictionary(text: myMutableArray) as? [AnyObject]
Thanks everyone for the amazing support, heres the answer to this bogus mess I created.
There are 2 parts to this: Serializing your array for transmission to your server, and then deserializing it from the server.
We need to see the code you use to serialize your array first. (The code that converts it to a string.) Based on what you post, it appears to be in JSON format. If so you may be able to skip the base64 encoding step, depending on what you're doing with the data. JSON is a good format for transmission to remote servers. You don't typically base64 encode JSON before transmission.
If you are receiving JSON that was then base64 encoded, you'll need to un-encode it back to data, and then use the JSONSerializer class to convert the JSON back to objects like arrays or dictionaries:
let data = Data(base64Encoded: base64DataFromServer)
guard let object = try? JSONSerialization.jsonObject(with: data) else {
return nil
}
(Note that the JSON you posted contains a dictionary as the top-level object, not an array.)
Trying to use SwiftyJSON library, but struggling for a day or so now.
I got this JSON.
“blah”:[{“xdeltaY":0,"xdeltaX":0,"blah1":435,"blah2":"\/Numbers\/Numbers.001.png","height":693.2142857142857,"width":1200,"blah3":240}]
I read it in from a file and assign it like this?
let quickfix = String(data: response.1, encoding: String.Encoding.utf8)
let json2F = JSON(quickfix!)
It seems to work. I can print this.
json2F.debugDescription
But I can do next to nothing beyond that. It hasn't parsed seems, I think it is just a string.
I am unable to figure out how to access the data within it. I also tried this...
if let dataFromString = quickfix?.data(using: .utf8, allowLossyConversion: false) {
let json2E = JSON(data: dataFromString)
print("json2E debugDescription \(json2F.debugDescription)")
}
But it returns null!! How to use this library?
You code looks good to me. It seems that your JSON is not valid.
Try to put an encapsulating object {} around it. Like this:
{"blah": [{"xdeltaY":0, "xdeltaX":0, "blah1":435, "blah2":"\/Numbers\/Numbers.001.png", "height":693.2142857142857, "width":1200, "blah3":240}]}
And additionally make sure you are using the right "not ”.
String+JSON
I also like to share a extension I use for getting my JSON from a string, which also gives you error messages in the console logs to prevent invalid JSON to mess up your program.
import Foundation
import SwiftyJSON
public extension String {
public func toJSON() -> JSON? {
if let data = self.data(using: String.Encoding.utf8, allowLossyConversion: false) {
var jsonError: NSError?
let json = JSON(data: data, error: &jsonError)
if jsonError == nil {
return json
} else {
print(jsonError!.description)
}
}
return nil
}
public func toJSONValue() -> JSON {
return self.toJSON() ?? JSON.null
}
}
I'm having an issue getting a JSON array using Swift.
func getBenefitJSON() -> [AnyObject] {
let urlString = String("https://www.kyfb.com/KYFB/includes/themes/CodeBlue/display_objects/custom/remote/webservices/services.cfc?method=getMemberBenefits")
let url = URL(string: urlString!)
var data = Data()
do {
data = try Data(contentsOf: url!)
} catch {
}
print("URL Data: \(data)")
do {
let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions()) as! [AnyObject]
print("JSON: \(json)")
return json
} catch {
print("Could not get JSON")
return []
}
}
I get the data from the url but get an empty array returned. What am I doing wrong?
URL Data: 42769 bytes
Could not get JSON
Below is the response of JSON
[{"image":"https://cdn.kyfb.com/KYFB/cache/file/70162997-E06B-E9B6-88514280CA8397CC_medium.jpg","description":"","link":"https://www.kyfb.com/insurance/insurance-products/","name":"KFB Insurance","children":[]}, ...]
Below is the error
UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
I've also validated the response from the URL at jsonlint.com
URL data as string:
<wddxPacket version='1.0'><header/><data><string>[{"image":"https://cdn.kyfb.com/KYFB/cache/file/70162997-E06B-E9B6-88514280CA8397CC_medium.jpg","description":"","link":"https://www.kyfb.com/insurance/insurance-products/","name":"KFB Insurance","children":[]}, ...]</string></data></wddxPacket>)
I tested your code and it is working if the top level of the JSON is an Array. So the JSON you want to read is probably not an array but a dictionary.
To read the Dictionary you need to change the return type from [AnyObject] into [String:AnyObject] and all other occurrences of this type.
Nevertheless, I would recommend to use SwiftyJSON for working with JSON in Swift.