Ive tried a lot of libraries, Alamofire, JsonHelper, ObjectMapper etc..., but unfortunately, Ive coundn't map a json collection response into an object class.
Im developing an IOS 8 App with swift 1.2 and xcode 6.3, and two classes of my model are:
Club.swift
class Club {
var id: String = ""
var name: String = ""
var imageUrl: String = ""
var hasVip: Bool = false
var desc: String = ""
var location: [Location] = []
}
Location.swift
class Location {
var country: String = ""
var city: String = ""
var address: String = ""
var zip: String = ""
var underground: [String] = []
}
I have another class to request to my API:
apliClient.swift
class ApiClient {
var clubs = [Club]?()
func getList(completionHandler: ([JSON]) -> ()) {
let URL = NSURL(string: "https://api.com/v1/clubs")
let mutableURLRequest = NSMutableURLRequest(URL: URL!)
mutableURLRequest.setValue("Content-Type", forHTTPHeaderField: "application/json")
mutableURLRequest.HTTPMethod = "GET"
mutableURLRequest.setValue("Bearer R01.iNsG3xjv/r1LDkhkGOANPv53xqUFDkPM0en5LIDxx875fBjdUZLn1jtUlKVJqVjsNwDe1Oqu2WuzjpaYbiWWhw==", forHTTPHeaderField: "Authorization")
let manager = Alamofire.Manager.sharedInstance
let request = manager.request(mutableURLRequest)
request.responseJSON { (request, response, json , error) in
if (json != nil){
var jsonObj = JSON(json!)
if let data = jsonObj["hits"].arrayValue as [JSON]?{
completionHandler(data)
}
}
}
}
}
and I think, there is a simple way to mapping objects in swift. I would like to know, how I can return the completionHandler(data) converted into a [Club] object?
let data = jsonObj["hits"].arrayValue as [JSON]? is
[{
"_id" : "5470def9e0c0be27780121d7",
"imageUrl" : "https:\/\/s3-eu-west-1.amazonaws.com\/api-static\/clubs\/5470def9e0c0be27780121d7_180.png",
"name" : "Mondo",
"hasVip" : false,
"location" : {
"city" : "Madrid"
}
}, {
"_id" : "540b2ff281b30f3504a1c72f",
"imageUrl" : "https:\/\/s3-eu-west-1.amazonaws.com\/api-static\/clubs\/540b2ff281b30f3504a1c72f_180.png",
"name" : "Teatro Kapital",
"hasVippler" : false,
"location" : {
"address" : "Atocha, 125",
"city" : "Madrid"
}
}, {
"_id" : "540cd44581b30f3504a1c73b",
"imageUrl" : "https:\/\/s3-eu-west-1.amazonaws.com\/api-static\/clubs\/540cd44581b30f3504a1c73b_180.png",
"name" : "Charada",
"hasVippler" : false,
"location" : {
"address" : "La Bola, 13",
"city" : "Madrid"
}
}]
You can do this with the ObjectMapper library that you mentioned above. Simply create the Club class and make sure it implements the Mappable protocol. Then you can use ObjectMapper as follows to map the data:
let clubs = Mapper<Club>().mapArray(JSONString)
Full disclosure: I am the author of ObjectMapper.
With Swift 2.0, it is now possible, simple copy your json to http://www.json4swift.com and the swift models with entire key-value mapping will be auto generated, all you need to do is instantiate the models by passing either array or dictionary out of your Json.
For objective-c try JSONModel it can be what you are looking for...
and here you can find some more example of using it
note this about swift (from JSONModel's GitHub page):
Swift works in a different way under the hood than Objective-C. Therefore I can't find a way to re-create JSONModel in Swift. JSONModel in Objective-C works in Swift apps through CocoaPods or as an imported Objective-C library.
Update:
Check ups this blog post from apple about Working with JSON in Swift
https://developer.apple.com/swift/blog/?id=37
Related
I am uploading multiple images as well as JSON data using "Content-type": "multipart/form-data"
my issue is, I have nested objects to pass to the parameters
I was looking for a solution, and what I find is nested data with an array of String or Int, not another custom object (I was struggling with it for so long)
struct Car: Codable{
var id :Int,
var name:String,
var address:String,
var new:Bool
var users:[User]
}
struct User: Codable{
var id :Int,
var name:String,
var address:String,
var age:Int
}
I wanted to convert the data to do a dictionary to use it as parameters
func addNewCae(newCar:Car, images:[UIImage]){
let encoder = JSONEncoder()
let jsonData = try! encoder.encode(newCar)
let test = convertStringToDictionary(text: jsonData)
print(test)
let headers: HTTPHeaders
headers = ["Content-type": "multipart/form-data",
"Content-Disposition" : "form-data"]
AF.upload(multipartFormData: { multipartFormData in
for imageData in images {
guard let imgData = imageData.pngData() else { return }
multipartFormData.append(imgData , withName: "images[]", fileName: "image.jpeg", mimeType: "image/jpeg")
}
for (key, value) in self.convertStringToDictionary(text: jsonData)! {
multipartFormData.append("\(value)".data(using: .utf8)!, withName: key)
}
},to: "\(url)", usingThreshold: UInt64.init(),
method: .post,
headers: headers).responseString{ response in
switch response.result {
case .success(let value):
print(value)
break
case .failure(let error):
print(error)
}
}
}
func convertStringToDictionary(data: Data) -> [String:AnyObject]? {
do {
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String:AnyObject]
return json
} catch {
print("Something went wrong")
}
return nil
}
As a request of my code: all the car fields are registered in the database except the users' field
(in reality, I have more fields than that)
Why the users' field is not added to the parameters?
Any idea?
The users filed normally be of type [[String: AnyObject]] in the parameters
here is the data after I convert it to a dictionary
Optional(["name":"whatever" , "address": "here", "users": <__NSArrayM 0x6000008e4f90>(
{
"name" = "hello";
"address" = "there";
"age" = 20;
},
{
"name" = "hi";
"address" = "location";
"age" = 25;
}
)
, "new": 0])
hope am clear enough, I can add any further code or information if needed to be clearer
Thanks
Update
I did the encoding manually so now I have a correct dictionary format of type [String: AnyObject] but one of them is nested :
["name":"whatever" , "address": "here", "users": [
[
"name" = "hello";
"address" = "there";
"age" = 20;
],
[
"name" = "hi";
"address" = "location";
"age" = 25;
]
]
, "new": 0]
BUT, still, the "users" field is not able to be read, I think because the parameters don't support that kind of type?
Anyone have any other idea how to deal with these nested objects?
Super new to swift, JSON, and pretty all coding so I apologize in advance if this question is redundant to others on the site or I am missing something simple here.
I am looking to return the value of "text" ("1.7 mi") associated with "distance" in the "elements" array in the JSON code below:
{
"destination_addresses" : [ "30 Rockefeller Plaza, New York, NY 10112, USA" ],
"origin_addresses" : [ "352 7th Ave, New York, NY 10001, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "1.7 mi",
"value" : 2729
},
"duration" : {
"text" : "15 mins",
"value" : 887
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
I retrieved the JSON data using Alamofire and the Google DistanceMatrix (see my code below), but am having trouble parsing the data to isolate what I need. I know the code below isn't close to what I need, but am unsure as to how to proceed.
func distanceMatrix(startLocation: String, endLocation: String) {
let myOrigin = startLocationTFText
let myDestination = destinationLocationTFText
let url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=\(myOrigin)&destinations=\(myDestination)&key=API_Key
let encodedUrl = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
AF.request(encodedUrl!).responseJSON { response in
print(response.request as Any)
print(response.response as Any)
print(response.data as Any)
print(response.result as Any)
let json = JSON(response.data as Any)
Any help is much appreciated. Thank you.
You can use Decodable to get the desired result.
struct RootResponse: Decodable {
let destinationAddresses, originAddresses: [String]
let rows: [Rows]
let status: String
}
struct Rows: Decodable {
let elements: [Elements]
}
struct Elements: Decodable {
let distance, duration: Details
let status: String
}
struct Details: Decodable {
let text, value: String
}
This will be your model file and once you have added it then you can go back to your function and use it as:
func distanceMatrix(startLocation: String, endLocation: String) {
let myOrigin = startLocationTFText
let myDestination = destinationLocationTFText
let url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=\(myOrigin)&destinations=\(myDestination)&key=API_Key"
let encodedUrl = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
AF.request(encodedUrl!).responseJSON { response in
print(response.request as Any)
print(response.response as Any)
print(response.data as Any)
print(response.result as Any)
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
guard let json = try? decoder.decode(RootResponse.self, from: response.data) else { print("Unable to parse JSON"); return }
print(json)
print(json.rows.first?.elements.first?.distance.value) // This is how you can get the value, but it will be better to safely unwrap them and I have also used first? to get the first object but this is an array and you can always use a for loop for further purpose
}
I want to extract the Event section from a wikipedia article: https://en.wikipedia.org/wiki/January_4
But I Can't figure out how to do it. I am using SwiftyJSON and Alamofire.
I have managed to use extracts to get the first paragraph of the article with this code:
func requestWikiInfo(pickedDate : String){
let parameters : [String:String] = [
"format" : "json",
"action" : "query",
"prop" : "extracts",
"exintro" : "",
"explaintext" : "",
"titles" : pickedDate,
"indexpageids" : "",
"redirects" : "1"
]
Alamofire.request(wikipediaURL, method: .get, parameters: parameters).responseJSON { (response) in
if response.result.isSuccess{
print("We got the wikipedia info")
//print(response)
let dateJSON : JSON = JSON(response.result.value!)
print(JSON(response.result.value))
let pageid = dateJSON["query"]["pageids"][0].stringValue
let dateDescription = dateJSON["query"]["pages"][pageid]["extract"].stringValue
self.dayDescriptionText.text = dateDescription
}
}
}
Thank you.
Use this: https://en.wikipedia.org/w/api.php?format=json&action=parse&page=January_4&prop=text§ion=1
Removing §ion=1 will return all the wikitext rather than just the events section (is this always the first section of the pages?). See the parse docs if you want to tweak it further.
I try to call the POST Api with Alamofire but it's showing me an error of incorrect format.
This is my JSON response:
[
{
"_source": {
"nome": "LOTERIAS BELEM",
"endereco": "R DO COMERCIO, 279",
"uf": "AL",
"cidade": "BELEM",
"bairro": "CENTRO"
},
"_id": "010177175"
},
{
"_source": {
"nome": "Bel Loterias"
},
"_id": "80224903"
},
{
"_source": {
"nome": "BELLEZA LOTERIAS",
"endereco": "R RIVADAVIA CORREA, 498",
"uf": "RS",
"cidade": "SANTANA DO LIVRAMENTO",
"bairro": "CENTRO"
},
"_id": "180124986"
}
]
class Album: Codable {
var _source : [_source]
}
class _source: Codable {
var nome : String
var endereco : String
var uf : String
var cidade : String
var bairro : String
}
var arrList = [Album]()
And this is how i try to Decoding with Alamofire.
func request() {
let urlString = URL(string: "My Url")
// Alamofire.request(url!).responseJSON {(response) in
Alamofire.request(urlString!, method: .post, parameters: ["name": "belem"],encoding: JSONEncoding.default, headers: nil).responseJSON {
(response) in
switch (response.result) {
case .success:
if let data = response.data {
do {
let response = try JSONDecoder().decode([Album].self, from: data)
DispatchQueue.main.async {
self.arrList = response
}
}
catch {
print(error.localizedDescription)
}
}
case .failure( let error):
print(error)
}
}
}
Just your Album model is incorrect.
struct Album: Codable {
var source : Source
var id : String
enum CodingKeys: String, CodingKey {
case source = "_source"
case id = "_id"
}
}
struct Source: Codable {
var nome : String
var endereco : String?
var uf : String?
var cidade : String?
var bairro : String?
}
If you don't want _id altogether then simply remove the related parts.
As for your Alamofire related code, that part is good.
Notable improvements:
Have avoided underscored variable name in model by customizing CodingKeys for key mapping purpose
Typenames should always start with a Capital letter (so _source is Source)
Similarly, variable names should always start with a lowercase letter
Made some variables optional (based on your updated response)
Keeping a variable non-optional means it must be present in the response for the model to be created
Making a variable optional means that key may or may not be present in the response and it not being there won't prevent the model from being created
I would like to recommend you to use json4swift.com. You just have to copy your json and paste there. It will automatically create modal struct or class from your json.
Coming back to your question, Your class Album doesn't have array of [_source]. That's the reason you are getting following error "The data couldn’t be read because it isn’t in the correct format".
Try below given format of album class,
class Album: Codable
{
var source: Source?
var id: String?
}
Please try to avoid using underscore in Swift.
I have a problem since last 2 days. I can't get my JSON transformed to a Realm Object.
I have a json like below:
{
"gender" : "male",
"id" : "123456789",
"age_range" : {
"min" : 21
},
"last_name" : "LastName"
}
I have this Realm Models:
class UserObject: Object {
dynamic var userId: String = ""
dynamic var lastName: String?
dynamic var gender: String?
var ageRange = List<AgeRangeObject>()
required convenience init?(_ map: Map) {
self.init()
}
}
class AgeRangeObject: Object {
dynamic var min: Int = 0
}
And the way I am trying to create an instance of this model with ObjectMapper to parse json to dictionary and then create the model instance:
let userJSONModel = Mapper<User>().map(jsonString)
let realm = try! Realm()
do {
try realm.write {
let dict: [String : AnyObject] = [
"userId" : (userJSONModel?.userId)!,
"ageRange" : (userJSONModel?.ageRange)!,
"lastName" : (userJSONModel?.lastName)!,
"gender" : (userJSONModel?.gender)!
]
let userModel = UserObject(value: dict)
realm.add(userModel)
}
} catch {
print("Exception")
}
The problem occurs on this line: let userModel = UserObject(value: dict)
I get the folowing error:
*** Terminating app due to uncaught exception 'RLMException', reason: 'Invalid value 'min' to initialize object of type 'AgeRangeObject': missing key 'min''
I was looking on the stackoverflow:
Nested Arrays throwing error in realm.create(value: JSON) for Swift
How to convert Realm object to JSON with nested NSDate properties?
but my case is different.
Do you know what's the problem with that age range dictionary? Why it can't parse it well?
Thank you.
In your JSON, ageRange is a dictionary, whereas the UserObject.ageRange property is a List<AgeRangeObject>. You have mismatched models.
You either need to update your models to reflect the structure of your JSON:
var ageRange = List<AgeRangeObject>()
becomes
dynamic var ageRange: AgeRangeObject? = nil
or vice versa, update your JSON to reflect the structure of your models:
{
"gender" : "male",
"id" : "123456789",
"age_range" : [{
"min" : 21
}],
"last_name" : "LastName"
}
{
"key1" : "value1",
"key2" : "value2",
"array1" : [{
"key" : value
}],
"key3" : "value3"
}
For this you could use ObjectMapper's TransformType.
Reference: https://github.com/APUtils/ObjectMapperAdditions
My Code:
#objcMembers class RealmObject: Object, Mappable {
dynamic var listValues = List<MyRealmObject>()
required convenience init?(map: Map) {
self.init()
}
// Mappable
func mapping(map: Map) {
listValues <- (map["listValues"], RealmlistObjectTransform())
}
}
#objcMembers class MyRealmObject: Object, Mappable {
required convenience init?(map: Map) {
self.init()
}
// Mappable
func mapping(map: Map) {
}
}
class RealmlistObjectTransform: TransformType {
typealias Object = List<MyRealmObject> // My Realm Object here
typealias JSON = [[String: Any]] // Dictionary here
func transformFromJSON(_ value: Any?) -> List<MyRealmObject>? {
let list = List<MyRealmObject>()
if let actors = value as? [[String: Any]] {
let objects = Array<MyRealmObject>(JSONArray: actors)
list.append(objectsIn: objects)
}
return list
}
func transformToJSON(_ value: List<MyRealmObject>?) -> [[String: Any]]? {
if let actors = value?.sorted(byKeyPath: "").toArray(ofType: MyRealmObject.self).toJSON() {
return actors
}
return nil
}
}