Encoding string data back to JSON is giving an error - swift

I've removed jsonCallback ( and ) from the URL https://www.apple.com/support/systemstatus/data/developer/system_status_en_US.js using the below.
var dataString = String(data: data, encoding: .utf8)
dataString = dataString?.replacingOccurrences(of: "jsonCallback(", with: "")
dataString = dataString?.replacingOccurrences(of: ");", with: "")
let json = dataString?.data(using: .utf8)
let jsonData = try JSONEncoder().encode(json)
The error I'm getting back
typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary<String, Any> but found a string/data instead.", underlyingError: nil))
I can't find where the mismatch is happening at because when I look at dataString and piece it back together the JSON decoding model appears to match.
Here's the full code:
func fetchSystemStatus() async -> [SystemStatus] {
guard let url = URL(string: systemStatusURL) else {
return []
}
do {
let (data, response) = try await URLSession.shared.data(from: url)
// This is commented out data to try and gather developer system status
var dataString = String(data: data, encoding: .utf8)
dataString = dataString?.replacingOccurrences(of: "jsonCallback(", with: "")
dataString = dataString?.replacingOccurrences(of: ");", with: "")
let json = dataString?.data(using: .utf8)
let jsonData = try JSONEncoder().encode(json)
guard (response as? HTTPURLResponse)?.statusCode == 200 else {
print("\(#function) \(response)")
return []
}
let statusData = try JSONDecoder().decode(SystemStatus.self, from: jsonData)
return [statusData]
} catch {
print("\(#function) \(error)")
return []
}
}
Model:
// MARK: - SystemStatus
struct SystemStatus: Codable {
let services: [Service]
enum CodingKeys: String, CodingKey {
case services = "services"
}
}
// MARK: - Service
struct Service: Codable, Identifiable {
let id = UUID()
let redirectURL: String?
let events: [Event]
let serviceName: String
enum CodingKeys: String, CodingKey {
case redirectURL = "redirectUrl"
case events = "events"
case serviceName = "serviceName"
}
}
// MARK: - Event
struct Event: Codable {
let usersAffected: String
let epochStartDate: Int
let epochEndDate: Int
let messageID: String
let statusType: String
let datePosted: String
let startDate: String
let endDate: String
let affectedServices: [String]?
let eventStatus: String
let message: String
enum CodingKeys: String, CodingKey {
case usersAffected = "usersAffected"
case epochStartDate = "epochStartDate"
case epochEndDate = "epochEndDate"
case messageID = "messageId"
case statusType = "statusType"
case datePosted = "datePosted"
case startDate = "startDate"
case endDate = "endDate"
case affectedServices = "affectedServices"
case eventStatus = "eventStatus"
case message = "message"
}
}

This should work:
let statusData = try JSONDecoder().decode(SystemStatus.self, from: Data(json!))
or
let statusData = try JSONDecoder().decode(SystemStatus.self, from: Data(dataString!.utf8))
What's your issue:
let jsonData = try JSONEncoder().encode(json)
But json here is Data, so if you call JSONEncoder on it, by default, it will use Base64, so it won't be like the JSON you expect.
But json already is correct.

Related

Swift: Get value from a JSON

I'm totally new with swift, it's my first iOs app
I would like to retrieve a value from an http POST response
struct represCode: Codable{
var CODE: String?
}
var table = [represCode]()
func httpPost(completion: #escaping (_ json: Any?)->()) {
let json: [String: Any] = ["login": usernameText.text!.uppercased(),
"pass": mdpText.text!]
let urlPath = url.chaine + "login.php"
let jsonData = try? JSONSerialization.data(withJSONObject: json)
let url = URL(string: urlPath)!
var request = URLRequest(url: url)
request.httpMethod = "POST"
// insert json data to the request
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
do {
self.table = try JSONDecoder().decode([represCode].self, from: data)
print(self.table)
self.dl = true
}catch _ {
print ("JSON Error")
}
completion(json)
}
task.resume()
}
When I "print(self.table)" I get this
[Mobois.LoginViewController.represCode(CODE: Optional("AG"))]
And I would like to store the "AG" in a specific var (ex: var represCode: String?)
I tried many solutions that I found here but most of time I get errors like "Cannot assign value of type '[LoginViewController.represCode]' to type 'String'"
There are two serious mistakes.
The root object is an array (represented by the [] in [represCode].self)
The value AG is the value for key CODE
First of all to conform to the naming convention declare the struct this way
struct RepresCode: Decodable {
let code: String
private enum CodingKeys: String, CodingKey { case code = "CODE" }
}
and
var table = [RepresCode]()
..
JSONDecoder().decode([RepresCode].self ...
You can access the value by getting the value for property code of the first item in the array
let represCode = table.first?.code ?? "unknown code"

Parsing JSON from URL ends up with an Error - Swift 5

I am trying to write a function in swift that gets data from an URL JSON, and allocate it to variables in swift.
This is the function:
func getBikeData(){
guard let url = URL(string: "https://api.citybik.es//v2/networks/baksi-bisim") else {return}
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data,
error == nil else {
print(error?.localizedDescription ?? "Response Error")
return }
do{
//here dataResponse received from a network request
let jsonResponse = try JSONSerialization.jsonObject(with:
dataResponse, options: [])
print(jsonResponse) //Response result
do {
//here dataResponse received from a network request
let decoder = JSONDecoder()
//Decode JSON Response Data
let model = try decoder.decode(Station.self,
from: dataResponse)
print(model.freeBikes) //Output - 1221
} catch let parsingError {
print("Error", parsingError)
}
} catch let parsingError {
print("Error", parsingError)
}
}
task.resume()
}
This is the struct's that I added, with the data I need:
// MARK: - Station
struct Station: Codable {
let emptySlots: Int
let extra: Extra
let freeBikes: Int
let id: String
let latitude, longitude: Double
let name, timestamp: String
enum CodingKeys: String, CodingKey {
case emptySlots
case extra
case freeBikes
case id, latitude, longitude, name, timestamp
}
}
// MARK: - Extra
struct Extra: Codable {
let slots: Int
let status: Status
let uid: String
}
enum Status: String, Codable {
case active = "Active"
}
This is the error I have been receiving:
Error keyNotFound(CodingKeys(stringValue: "emptySlots", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"emptySlots\", intValue: nil) (\"emptySlots\").", underlyingError: nil))
This is the first time I was working with a JSON file, and maybe I am missing something very simple. Please help.
Here is how to decode all the json data into a swift struct:
import Foundation
struct Stations: Codable {
let company: [String]
let href: String
let id: String
let location: LocationJson
let name: String
let stations: [Station]
}
struct Station: Codable {
let empty_slots: Int
let extra: Extra
let free_bikes: Int
let id: String
let latitude: Double
let longitude: Double
let name: String
let timestamp: String
}
struct ResponseJSON: Codable {
let network: Stations
}
struct LocationJson: Codable {
let city: String
let country: String
let latitude: Double
let longitude: Double
}
struct Extra: Codable {
let slots: Int
let status: String
let uid: String
}
func getBikeData(){
guard let url = URL(
string: "https://api.citybik.es//v2/networks/baksi-bisim"
) else { return }
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data, error == nil else {
print(error?.localizedDescription ?? "Response Error")
return
}
do {
//here dataResponse received from a network request
let jsonResponse = try JSONSerialization.jsonObject(with:
dataResponse, options: [])
print(jsonResponse) //Response result
do {
//here dataResponse received from a network request
let decoder = JSONDecoder()
//Decode JSON Response Data
let model = try decoder.decode(
ResponseJSON.self, from: dataResponse
)
print(model) //Output - 1221
} catch let parsingError {
print("Error", parsingError)
}
} catch let parsingError {
print("Error", parsingError)
}
}
task.resume()
}
getBikeData()
Error keyNotFound(CodingKeys(stringValue: "emptySlots", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"emptySlots\", intValue: nil) (\"emptySlots\").", underlyingError: nil))
The above error will occur if you are trying to decode a JSON object that doesn't have that key and the key is not marked as an optional.
You can either fix the server-end so emptySlots is returned or mark emptySlots as an optional in your struct:
struct Station: Codable {
let emptySlots: Int?

Data fetched via postman and URLSession is different in here http://173.249.20.137:9000/apiapp/coupon

http://173.249.20.137:9000/apiapp/coupon GET method .
when I request via URLSession and Postman I get two different results. Actually postman data is correct, but the URL session has always the same response whether I add or delete data it is not going to update. Can anybody please give a look. if it is happening with me only or something wrong at the backend server.
I have tested requesting data with URLSession.shared and postman.
I actually like to have the data I get via postman through URLSession request too.
func getAvailableCoupons(urlString:String, completion: #escaping (_
product: Any, _ error: Error?) -> Void){
guard let url = URL(string: urlString) else {return}
let task = URLSession.shared.dataTask(with: url) { (data, response,
error) in
let jsonDecoder = JSONDecoder()
guard let dataResponse = data,
error == nil else {
print(error?.localizedDescription ?? "Response Error")
return }
let statusCode = (response as! HTTPURLResponse).statusCode
let responseJSON = try? JSONSerialization.jsonObject(with: dataResponse, options: [])
if let responseJSON = responseJSON as? [String: Any] {
if statusCode == 200 {
do {
let jsonData = try JSONSerialization.data(withJSONObject: responseJSON, options: [])
let responseData = try jsonDecoder.decode(CoupensResponseModel.self, from:jsonData)
completion(responseData, nil)
} catch let error {
print("error when parshing json response \(error)")
completion(error, nil )
}
} else if statusCode == 404{
completion(" 404 not found", nil )
} else {
print("fatal error \(error?.localizedDescription ?? "big errror")")
}
}
}
task.resume()
}
import Foundation
// MARK: - CoupensResponseModel
struct CoupensResponseModel: Codable {
let couponDetails: [CouponDetail]?
enum CodingKeys: String, CodingKey {
case couponDetails = "coupon_details"
}
}
// MARK: - CouponDetail
struct CouponDetail: Codable {
let id: Int?
let vouchersusageSet: [VouchersusageSet]?
let couponCode: String?
let minimumSpend: Int?
let expiryDate, createdDate: String?
let discountPrice, discountPercent: Int?
let discountBasis: DiscountBasis?
let couponImage: String?
let couponType: String?
enum CodingKeys: String, CodingKey {
case id
case vouchersusageSet = "vouchersusage_set"
case couponCode = "coupon_code"
case minimumSpend = "minimum_spend"
case expiryDate = "expiry_date"
case createdDate = "created_date"
case discountPrice = "discount_price"
case discountPercent = "discount_percent"
case discountBasis = "discount_basis"
case couponImage = "coupon_image"
case couponType = "coupon_type"
}
}
enum DiscountBasis: String, Codable {
case amount = "amount"
case percent = "percent"
}
// MARK: - VouchersusageSet
struct VouchersusageSet: Codable {
let id: Int?
let itemObj: ItemObj?
let voucherObj: Int?
let sectionObj, categoryObj: Int?
}
// MARK: - ItemObj
struct ItemObj: Codable {
let id: Int?
let code, productName: String?
let productPrice, discountedPrice: Int?
let productDescription, itemImage: String?
let categoryObj: CouponCategoryObj?
enum CodingKeys: String, CodingKey {
case id, code
case productName = "product_name"
case productPrice = "product_price"
case discountedPrice = "discounted_price"
case productDescription = "product_description"
case itemImage = "item_image"
case categoryObj
}
}
// MARK: - CouponCategoryObj
struct CouponCategoryObj: Codable {
let id: Int?
let categoryCode, categoryName: String?
let categoryImage: CouponJSONNull?
let sectionObj: Int?
enum CodingKeys: String, CodingKey {
case id
case categoryCode = "category_code"
case categoryName = "category_name"
case categoryImage = "category_image"
case sectionObj
}
}
// MARK: - Encode/decode helpers
class CouponJSONNull: Codable, Hashable {
public static func == (lhs: CouponJSONNull, rhs: CouponJSONNull) ->
Bool {
return true
}
public var hashValue: Int {
return 0
}
public func hash(into hasher: inout Hasher) {
// No-op
}
public init() {}
public required init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if !container.decodeNil() {
throw DecodingError.typeMismatch(CouponJSONNull.self,
DecodingError.Context(codingPath: decoder.codingPath,
debugDescription:
"Wrong type for CouponJSONNull"))
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encodeNil()
}
}
Try the following method
let headers = [
"Cache-Control": "no-cache",
]
let request = NSMutableURLRequest(url: NSURL(string: "http://173.249.20.137:9000/apiapp/coupon")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let string = String(data: data!, encoding: .utf8) ?? ""
print(string)
}
})
dataTask.resume()
Please look on response both are same.
Postman response :
https://jsoneditoronline.org/?id=7b94ef69a3344164aa4a96423fdbf9db
Code response :
https://jsoneditoronline.org/?id=6e5a7d221d9c4c818f1d46fc893031fe

Swift 4 Codable - API provides sometimes an Int sometimes a String

I have Codables running now. But the API has some String entries that can sometimes have an Int value of 0 if they are empty. I was searching here and found this: Swift 4 Codable - Bool or String values But I'm not able to get it running
My struct
struct check : Codable {
let test : Int
let rating : String?
}
Rating is most of the time something like "1Star". But if there is no rating I get 0 as Int back.
I'm parsing the data like this:
enum Result<Value> {
case success(Value)
case failure(Error)
}
func checkStar(for userId: Int, completion: ((Result<check>) -> Void)?) {
var urlComponents = URLComponents()
urlComponents.scheme = "https"
urlComponents.host = "xyz.com"
urlComponents.path = "/api/stars"
let userIdItem = URLQueryItem(name: "userId", value: "\(userId)")
urlComponents.queryItems = [userIdItem]
guard let url = urlComponents.url else { fatalError("Could not create URL from components") }
var request = URLRequest(url: url)
request.httpMethod = "GET"
let config = URLSessionConfiguration.default
config.httpAdditionalHeaders = [
"Authorization": "Bearer \(keytoken)"
]
let session = URLSession(configuration: config)
let task = session.dataTask(with: request) { (responseData, response, responseError) in
DispatchQueue.main.async {
if let error = responseError {
completion?(.failure(error))
} else if let jsonData = responseData {
// Now we have jsonData, Data representation of the JSON returned to us
// from our URLRequest...
// Create an instance of JSONDecoder to decode the JSON data to our
// Codable struct
let decoder = JSONDecoder()
do {
// We would use Post.self for JSON representing a single Post
// object, and [Post].self for JSON representing an array of
// Post objects
let posts = try decoder.decode(check.self, from: jsonData)
completion?(.success(posts))
} catch {
completion?(.failure(error))
}
} else {
let error = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey : "Data was not retrieved from request"]) as Error
completion?(.failure(error))
}
}
}
task.resume()
}
Loading it:
func loadStars() {
checkStar(for: 1) { (result) in
switch result {
case .success(let goo):
dump(goo)
case .failure(let error):
fatalError(error.localizedDescription)
}
}
}
I hope someone can help me there, cause I'm not completely sure how this parsing, etc. works.
you may implement your own decode init method, get each class property from decode container, during this section, make your logic dealing with wether "rating" is an Int or String, sign all required class properties at last.
here is a simple demo i made:
class Demo: Decodable {
var test = 0
var rating: String?
enum CodingKeys: String, CodingKey {
case test
case rating
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let test = try container.decode(Int.self, forKey: .test)
let ratingString = try? container.decode(String.self, forKey: .rating)
let ratingInt = try? container.decode(Int.self, forKey: .rating)
self.rating = ratingString ?? (ratingInt == 0 ? "rating is nil or 0" : "rating is integer but not 0")
self.test = test
}
}
let jsonDecoder = JSONDecoder()
let result = try! jsonDecoder.decode(Demo.self, from: YOUR-JSON-DATA)
if rating API's value is normal string, you will get it as you wish.
if rating API's value is 0, rating will equal to "rating is nil or 0"
if rating API's value is other integers, rating will be "rating is integer but not 0"
you may modify decoded "rating" result, that should be easy.
hope this could give you a little help. :)
for more info: Apple's encoding and decoding doc

JSON encoding with backslashes

I m using Alamofire and SwiftyJSOn to parse JSON output. It works very well however some sites give json with escaped output. I use Alamofire like below
Alamofire.request(.POST, url, parameters: param, encoding: .JSON)
.responseJSON { (req, res, json, error) in
var json = JSON(json!)
Site gives me JSON result with escaped string so SwiftyJSON can't decode it. How can I convert below
{
"d": "{\"UniqeView\":{\"ArrivalDate\":null,\"ArrivalUnitId\":null,\"DeliveryCityName\":null,\"DeliveryTownName\":null},\"ErrorMessage\":null,\"Message\":null,\"IsFound\":false,\"IsSuccess\":true}"
}
to something like
{
"d": {
"UniqeView": {
"ArrivalDate": null,
"ArrivalUnitId": null,
"DeliveryCityName": null,
"DeliveryTownName": null
},
"ErrorMessage": null,
"Message": null,
"IsFound": false,
"IsSuccess": true
}
}
// This Dropbox url is a link to your JSON
// I'm using NSData because testing in Playground
if let data = NSData(contentsOfURL: NSURL(string: "https://www.dropbox.com/s/9ycsy0pq2iwgy0e/test.json?dl=1")!) {
var error: NSError?
var response: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: &error)
if let dict = response as? NSDictionary {
if let key = dict["d"] as? String {
let strData = key.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
var error: NSError?
var response: AnyObject? = NSJSONSerialization.JSONObjectWithData(strData!, options: NSJSONReadingOptions.allZeros, error: &error)
if let decoded = response as? NSDictionary {
println(decoded["IsSuccess"]!) // => 1
}
}
}
}
I guess you have to decode twice: the wrapping object, and its content.
#ericd comments helped me to solve the issue. I accepted his answer for this question. Since I am using Alamofire for asynchronous operation, and SwiftyJSON, I couldn't use his code. Here is the code with Alamofire and SwiftyJSON.
Alamofire.request(.POST, url, parameters: param, encoding: .JSON)
.responseJSON { (req, res, json, error) in
if(error != nil) {
NSLog("Error: \(error)")
failure(res, json, error)
}
else {
var jsond = JSON(json!)
var data = jsond["d"].stringValue.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
jsond = JSON(data: data!)
Lots of people have problems distinguishing between what they get and what their system prints. So first step is you need to find out what exactly you are receiving, and whether or not these escape characters are just an artefact of you printing.
If this is what you actually receive, then the server has sent you a dictionary with a single key "d" and a string, and the string contains serialized data. In that case, convert the string to NSData and shove it into NSJSONSerialization, which will turn it into the dictionary that you want. This is a rather stupid way to transmit JSON data, but it happens.
Here is another approach for Swift 4 - Using Codable
This was the json that I received:
{
"error_code": 0,
"result": {
"responseData": "{\"emeter\":{\"get_realtime\":{\"voltage_mv\":237846,\"current_ma\":81,\"power_mw\":7428,\"total_wh\":1920,\"err_code\":0}}}"
}
}
The JSON part with backslashes is equal to this:
{
"emeter": {
"get_realtime": {
"voltage_mv": 237846,
"current_ma": 81,
"power_mw": 7428,
"total_wh":19201,
"err_code":0
}
}
}
And this was the code that I used:
import Foundation
class RealtimeEnergy: Codable {
let errorCode: Int
let result: ResultRealtimeEnergy?
let msg: String?
enum CodingKeys: String, CodingKey {
case errorCode = "error_code"
case result, msg
}
init(errorCode: Int, result: ResultRealtimeEnergy?, msg: String?) {
self.errorCode = errorCode
self.result = result
self.msg = msg
}
}
class ResultRealtimeEnergy: Codable {
let responseData: String
var emeter: Emeter
enum CodingKeys: String, CodingKey {
case responseData
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
responseData = try container.decode(String.self, forKey: .responseData)
let dataString = try container.decode(String.self, forKey: .responseData)
emeter = try JSONDecoder().decode(Emeter.self, from: Data(dataString.utf8))
}
}
class Emeter: Codable {
let emeter: EmeterClass
init(emeter: EmeterClass) {
self.emeter = emeter
}
}
class EmeterClass: Codable {
let getRealtime: GetRealtime
enum CodingKeys: String, CodingKey {
case getRealtime = "get_realtime"
}
init(getRealtime: GetRealtime) {
self.getRealtime = getRealtime
}
}
class GetRealtime: Codable {
let voltageMv, currentMa, powerMw, totalWh: Int
let errCode: Int
enum CodingKeys: String, CodingKey {
case voltageMv = "voltage_mv"
case currentMa = "current_ma"
case powerMw = "power_mw"
case totalWh = "total_wh"
case errCode = "err_code"
}
init(voltageMv: Int, currentMa: Int, powerMw: Int, totalWh: Int, errCode: Int) {
self.voltageMv = voltageMv
self.currentMa = currentMa
self.powerMw = powerMw
self.totalWh = totalWh
self.errCode = errCode
}
}
And this is the trick:
emeter = try JSONDecoder().decode(Emeter.self, from: Data(dataString.utf8))
i use some custom function to do this work:
import Foundation
func unescapeString(string: String) -> String {
return string.stringByReplacingOccurrencesOfString("\"", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
}
I hope it helps ;)
I spent way too long trying to figure out the same issue. Here's how I solved it.
I have a network manager that when called, returns a response of [Any]?
I loop through each record, converting it to JSON, but that doesn't recognize the inherent dictionary structure in this case.
So I pluck the rawString and then use parse. This does recognize the dictionary.
From there you should be able to use it as you would. In my example, I pass the data to a data model (MyApi),
networkManager .requestResource(withUrl: urlPath, andParams: params,
successHandler: { (response: [Any]?) in
if let rawResponse = response {
let mutableArray = NSMutableArray()
for item in rawResponse {
let jsonData = JSON(item)
guard let rawString = jsonData.rawString() else {
return
}
let parsedData = JSON.parse(rawString)
let typedResponse = MyApi(json: parsedData)
mutableArray.add(typedResponse)
}
let array = mutableArray.copy() as! [MyApi]
//do something with array
} else {
let error = NSError .init(domain: "MyApi", code: 100, userInfo: nil)
//log error
}
}, failureHandler: { (response: [Any]?) in
let error = NSError .init(domain: "MyApi", code: 101, userInfo: nil)
//log error
})