JSON encoding with backslashes - swift

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
})

Related

Parsing JSON to a struct with SwiftyJSON

I have a simple struct to handle the data parsing from the SwiftyJSON
struct Threads{
var threads:[ThreadDetail]
}
struct ThreadDetail {
var title:String
var username:String
var prefix_id:Int
}
Here's a sample of the API response
{
"threads": [
{
"first_post_id": 258535,
"prefix_id": 1,
"thread_id": 50204,
"title": "Testing board title",
"user_id": 20959,
"username": "test",
"view_count": 247,
}
Now here's the part where I couldn't figure out how
Alamofire.request(url, method: .get, headers: headers).validate().responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
for item in json["threads"].arrayValue {
//how should it be written here?
}
case .failure(let error):
print(error)
}
}
}
}
Declare your models and conform them Codable.
struct Response: Codable {
let threads: [Thread]
}
// MARK: - Thread
struct Thread: Codable {
let firstPostID, prefixID, threadID: Int
let title: String
let userID: Int
let username: String
let viewCount: Int
enum CodingKeys: String, CodingKey {
case firstPostID = "first_post_id"
case prefixID = "prefix_id"
case threadID = "thread_id"
case title
case userID = "user_id"
case username
case viewCount = "view_count"
}
}
After that, convert your data to model by using JSONDecoder
Alamofire.request(urlString).response {
response in
guard let data = response.data else { return }
do {
let decoder = JSONDecoder()
let threadsWrapper = try decoder.decode(Response.self, from: data)
} catch let error {
print(error)
}
Use this website to convert your JSON to Codable
https://app.quicktype.io/
You can do it by following as far as I remember SwiftyJSON:
Alamofire.request(url, method: .get, headers: headers).validate().responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
var models = [ThreadDetail]()
for item in json["threads"].arrayValue {
let model = ThreadDetail(title: item["title"].stringValue,
username: item["username"].stringValue,
prefix_id: item["prefix_id"].intValue)
models.append(model)
}
// do whatever you need with models
case .failure(let error):
print(error)
}
}
But really, as like Frankenstein said, the best way to solve your problem is just to conform your models to Codable.
I personally use a library I custom made for apps that use JSON called Weave. It's built on top of NSURLSession and fully native Swift. When you create a request using WVRequest.request(), for the argument outputType, use the type .json.
Here's your above code using Weave:
WVRequest.start(url, requestType: .get, outputType:.json, headers: headers).start() { response in
if response.success {
// Cast to a dictionary
let json = (response as! WVJSONResponse).json as! [String:Any]
let threads = json["threads"] as! [Thread]
// From here, just use a for loop, and create an instance of your Thread class for each iteration.
} else {
print("Error!")
}
}
Hope that helps!
struct Temperature: Codable {
let high: Double
let low: Double
}
struct Bluh: Codable, Identifiable {
var temperature: JSON
func getTemperatures() -> Temperature? {
do {
let decoder = JSONDecoder()
return try decoder.decode(Temperature.self, from: temperature.rawData())
} catch let error {
print(error)
}
}
}
You can do sth like tis too. If your data is SWIFY JSON and trying to convert to struct.

Passing value from Alamofire get request to variable

Here is my code that parse data but I want to get jsonDictionary and assign it to another variable for using outer scope.
I've checked many answers but almost all are use print for showig outcome , I need to get value instead of showing it in console.
Can anyone help me please to solve it
public func fetchData() {
let request = Alamofire.request("www.anyurl.com")
request.responseJSON { data in
if let json = data.result.value as? [String: Any] {
guard let jsonArray = json["data"] as? [[String: Any]] else {
return
}
if let jsonDictionary = jsonArray[0]["title"] as? [[String:Any]] {
print(jsonDictionary)
}
}
}
}
As a example we get this simple Json Response.
{
"success": true,
"Message": "data retrieved successfully",
"data": {
"name": "Dilan",
"age": "26",
"country": "Sri Lanka"
}
}
simply you can define public variable in somewhere in your project like
public static var user:NSDictionary = [:]
Then you can directly assign your json response as a dictionary to that variable and use it in your project.
AF.request(URL(string: "someurl")!,
method: .get,
headers:AppConstants.headers)
.validate(statusCode:200..<300)
.validate(contentType:[AppConstants.contentTypeJson])
.responseJSON { (response) in
switch response.result{
case .success(let data):
guard let json = data as? [String:AnyObject] else {
return
}
let isSuccess = (json["success"])?.boolValue ?? false
if isSuccess {
SomeClass.user = json["data"] as? NSDictionary ?? [:]
}else{
print("false")
}
case .failure(let error):
print("Error")
}
}
This is the another way to do that using Codable,Decodable
first create Codable Struct
import Foundation
// MARK: - User
struct User: Codable {
let success: Bool
let message: String
let data: DataClass
enum CodingKeys: String, CodingKey {
case success
case message = "Message"
case data
}
}
// MARK: - DataClass
struct DataClass: Codable {
let name, age, country: String
}
then you can convert your response directly to codable object
AF.request(URL(string: "someurl")!,
method: .get,
headers:AppConstants.headers)
.validate(statusCode:200..<300)
.validate(contentType:[AppConstants.contentTypeJson])
.responseJSON { (response) in
switch response.result{
case .success(let data):
let decoder = JSONDecoder()
do {
SomeClass.User= try decoder.decode(User.self, from: data)
//this time user should be public static var user:User?
} catch {
print(error.localizedDescription)
}
case .failure(let error):
print("Error")
}
}

Decoding dynamic JSON structure in swift 4

I have the following issue that I'm not sure how to handle.
My JSON response can look like this:
{
"data": {
"id": 7,
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NDY1MTU0NDMsImRhdGEiOiJ2bGFkVGVzdCIsImlhdCI6MTU0NjUwODI0M30.uwuPhlnchgBG4E8IvHvK4bB1Yj-TNDgmi7wUAiKmoVo"
},
"error": null
}
Or like this:
{
"data": [{
"id": 12
}, {
"id": 2
}, {
"id": 5
}, {
"id": 7
}],
"error": null
}
So in short the data can be either a single objet or an Array. What i have is this:
struct ApiData: Decodable {
var data: DataObject?
var error: String?
}
struct DataObject: Decodable {
var userId: Int?
enum CodingKeys: String, CodingKey {
case userId = "id"
}
}
This works fine for the first use case, but it will fail once data turns into
var data: [DataObject?]
How do I make that dynamic without duplicating code?
Edit: This is how i decode the object as well
func makeDataTaskWith(with urlRequest: URLRequest, completion: #escaping(_ apiData: ApiData?) -> ()) {
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
session.dataTask(with: urlRequest) {
(data, response, error) in
guard let _ = response, let data = data else {return}
if let responseCode = response as? HTTPURLResponse {
print("Response has status code: \(responseCode.statusCode)")
}
do {
let retreived = try NetworkManager.shared.decoder.decode(ApiData.self, from: data)
completion(retreived)
} catch let decodeError as NSError {
print("Decoder error: \(decodeError.localizedDescription)\n")
return
}
}.resume()
}
If data can be a single object or an array write a custom initializer which decodes first an array, if a type mismatch error occurs decode a single object. data is declared as an array anyway.
As token appears only in a single object the property is declared as optional.
struct ApiData: Decodable {
let data : [DataObject]
let error : String?
private enum CodingKeys : String, CodingKey { case data, error }
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
do {
data = try container.decode([DataObject].self, forKey: .data)
} catch DecodingError.typeMismatch {
data = [try container.decode(DataObject.self, forKey: .data)]
}
error = try container.decodeIfPresent(String.self, forKey: .error)
}
}
struct DataObject: Decodable {
let userId : Int
let token : String?
private enum CodingKeys: String, CodingKey { case userId = "id", token }
}
Edit: Your code to receive the data can be improved. You should add a better error handling to return also all possible errors:
func makeDataTaskWith(with urlRequest: URLRequest, completion: #escaping(ApiData?, Error?) -> Void) {
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
session.dataTask(with: urlRequest) {
(data, response, error) in
if let error = error { completion(nil, error); return }
if let responseCode = response as? HTTPURLResponse {
print("Response has status code: \(responseCode.statusCode)")
}
do {
let retreived = try NetworkManager.shared.decoder.decode(ApiData.self, from: data!)
completion(retreived, nil)
} catch {
print("Decoder error: ", error)
completion(nil, error)
}
}.resume()
}
Using power of generic, it simple like below:
struct ApiData<T: Decodable>: Decodable {
var data: T?
var error: String?
}
struct DataObject: Decodable {
private var id: Int?
var userId:Int? {
return id
}
}
Use
if let obj = try? NetworkManager.shared.decoder.decode(ApiData<DataObject>.self, from: data) {
//Do somthing
} else if let array = try NetworkManager.shared.decoder.decode(ApiData<[DataObject]>.self, from: data) {
// Do somthing
}
If you have only two possible outcomes for your data, an option would be to try and parse data to one of the expected types, if that fails you know that the data is of other type and you can then handle it accordingly.
See this
You can try
struct Root: Codable {
let data: DataUnion
let error: String?
}
enum DataUnion: Codable {
case dataClass(DataClass)
case datumArray([Datum])
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let x = try? container.decode([Datum].self) {
self = .datumArray(x)
return
}
if let x = try? container.decode(DataClass.self) {
self = .dataClass(x)
return
}
throw DecodingError.typeMismatch(DataUnion.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for DataUnion"))
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .dataClass(let x):
try container.encode(x)
case .datumArray(let x):
try container.encode(x)
}
}
}
struct Datum: Codable {
let id: Int
}
struct DataClass: Codable {
let id: Int
let token: String
}
let res = try? JSONDecoder().decode(Root.self, from:data)

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

Swift is not printing or displaying name in App from a weather API?

if let jsonObj = jsonObj as? [String: Any],
let weatherDictionary = jsonObj["weather"] as? [String: Any],
let weather = weatherDictionary["description", default: "clear sky"] as?
NSDictionary {
print("weather")
DispatchQueue.main.async {
self.conditionsLabel.text = "\(weather)"
}
}
// to display weather conditions in "name" from Open Weather
"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}]
//No errors, but code is not printing or displaying in App.
I'm not sure how to help with your exact question unless you can provide some more code for context. However,
You might try using the built-in decoding that comes with Swift 4. Check it out here. Basically, you make a class that models the response object, like this:
struct Weather: Decodable {
var id: Int
var main: String
var description: String
var icon: String
}
Then decode it like so:
let decoder = JSONDecoder()
let weather = try decoder.decode(Weather.self, from: jsonObj)
And it magically decodes into the data you need! Let me know if that doesn't work, and comment if you have more code context for your problem that I can help with.
I put the complete demo here to show how to send a HTTP request and parse the JSON response.
Note, Configure ATS if you use HTTP request, rather than HTTPS request.
The demo URL is "http://samples.openweathermap.org/data/2.5/forecast?q=M%C3%BCnchen,DE&appid=b6907d289e10d714a6e88b30761fae22".
The JSON format is as below, and the demo shows how to get the city name.
{
cod: "200",
message: 0.0032,
cnt: 36,
list: [...],
city: {
id: 6940463,
name: "Altstadt",
coord: {
lat: 48.137,
lon: 11.5752
},
country: "none"
}
}
The complete demo is as below. It shows how to use URLSessionDataTask and JSONSerialization.
class WeatherManager {
static func sendRequest() {
guard let url = URL(string: "http://samples.openweathermap.org/data/2.5/forecast?q=M%C3%BCnchen,DE&appid=b6907d289e10d714a6e88b30761fae22") else {
return
}
// init dataTask
let dataTask = URLSession.shared.dataTask(with: url) { (data, response, error) in
let name = WeatherManager.cityName(fromWeatherData: data)
print(name ?? "")
}
// send the request
dataTask.resume()
}
private static func cityName(fromWeatherData data: Data?) -> String? {
guard let data = data else {
print("data is nil")
return nil
}
do {
// convert Data to JSON object
let jsonObject = try JSONSerialization.jsonObject(with: data, options: [])
print(jsonObject)
if let jsonObject = jsonObject as? [String: Any],
let cityDic = jsonObject["city"] as? [String: Any],
let name = cityDic["name"] as? String {
return name
} else {
return nil
}
} catch {
print("failed to get json object")
return nil
}
}
}