Reputation: 194
I'm trying to make the model class to handle the JSON response with dynamic keys and their values but getting an error i.e.
Incorrect argument label in call (have 'JSON:', expected 'map:') Replace 'JSON' with 'map', while parsing the data to the model class.
I'm using ObjectMapper
for a mapping JSON response
Below what I tried till now:
Top Courses Response Model to handle JSON Response Object
import Foundation
import ObjectMapper
struct StudentTopCouses: Mappable {
init?(map: Map) {}
var results: [String: [CoursesListObject]]?
mutating func mapping(map: Map) {
results <- map["data"]
}
}
class CoursesListObject: Mappable {
var id: NSNumber?
var createdOn: NSNumber?
var name: String?
var description: String?
var createdBy: String?
var approved: Bool?
var reason: String?
var courseStatus: String?
var courseCategory: CourseCategory?
required init?(map: Map) {}
func mapping(map: Map) {
id <- map["id"]
createdOn <- map["createdOn"]
name <- map["name"]
description <- map["description"]
createdBy <- map["createdBy"]
approved <- map["approved"]
reason <- map["reason"]
courseStatus <- map["courseStatus"]
courseCategory <- map["courseCategory"]
}
}
class CourseCategory: Mappable {
var id: NSNumber?
var createdOn: NSNumber?
var category: String?
var description: Any?
required init?(map: Map){
}
func mapping(map: Map) {
id <- map["id"]
createdOn <- map["createdOn"]
category <- map["category"]
description <- map["description"]
}
}
Function for Top Courses API Success:
func getTopCourseNetworkCallSuccess(data : JSON){
if data["isSuccess"].boolValue {
ActivityIndicator.shared.hideProgressView()
if let r = StudentTopCouses.init(JSON: data), let result = r.results {
for (key, value) in result {
print("Key: \(key)" )
print("Course Name: \(value.first!.name!)")
}
}
} else {
//Handle Error Response
}
}
Here is my API JSON Response like this
{
"data": {
"ManualTesting": [
{
"id": 1949,
"createdOn": 1572433478000,
"name": "Software Testing",
"description": "It's a type of software Testing",
"createdBy": "opp",
"approved": true,
"reason": "asdasdasdcassdqadaxasdasd",
"courseStatus": "APPROVED",
"courseCategory": {
"id": 1916,
"createdOn": 1572421487000,
"category": "ManualTesting",
"description": "Ultimate QA Manual Testing course with Live project and Live demo + Real time interview questions + Life time support"
}
}
],
"Java Technology": [
{
"id": 2064,
"createdOn": 1572609103000,
"name": "hfjfjf",
"description": "hchcjcf\n",
"createdBy": "Tq",
"approved": true,
"reason": null,
"courseStatus": "APPROVED",
"courseCategory": {
"id": 1910,
"createdOn": 1572346886000,
"category": "Java Technology",
"description": "Learn Java In This Course And Become a Computer Programmer. Obtain valuable Core Java Skills And Java Certification"
}
},
{
"id": 2159,
"createdOn": 1572868301000,
"name": "Spring beginner",
"description": "we will teach you a spring boot.",
"createdBy": "Haris",
"approved": true,
"reason": null,
"courseStatus": "APPROVED",
"courseCategory": {
"id": 1910,
"createdOn": 1572346886000,
"category": "Java Technology",
"description": "Learn Java In This Course And Become a Computer Programmer. Obtain valuable Core Java Skills And Java Certification"
}
},
{
"id": 2168,
"createdOn": 1572871357000,
"name": "Multi Threading",
"description": "we will teach you multi threading",
"createdBy": "Haris",
"approved": true,
"reason": null,
"courseStatus": "APPROVED",
"courseCategory": {
"id": 1910,
"createdOn": 1572346886000,
"category": "Java Technology",
"description": "Learn Java In This Course And Become a Computer Programmer. Obtain valuable Core Java Skills And Java Certification"
}
},
{
"id": 2169,
"createdOn": 1572871521000,
"name": "Hybernate ",
"description": "we will teach you hybernate",
"createdBy": "Haris",
"approved": true,
"reason": null,
"courseStatus": "APPROVED",
"courseCategory": {
"id": 1910,
"createdOn": 1572346886000,
"category": "Java Technology",
"description": "Learn Java In This Course And Become a Computer Programmer. Obtain valuable Core Java Skills And Java Certification"
}
},
{
"id": 2293,
"createdOn": 1573032019000,
"name": "Spring",
"description": "we will teach you spring",
"createdBy": "hard",
"approved": true,
"reason": "",
"courseStatus": "APPROVED",
"courseCategory": {
"id": 1910,
"createdOn": 1572346886000,
"category": "Java Technology",
"description": "Learn Java In This Course And Become a Computer Programmer. Obtain valuable Core Java Skills And Java Certification"
}
}
],
"Python Technology": [
{
"id": 2041,
"createdOn": 1572601461000,
"name": "Python for Data Science and Machine Learning Bootcamp",
"description": "Learn how to use NumPy, Pandas, Seaborn , Matplotlib , Plotly , Scikit-Learn , Machine Learning, Tensorflow , and more",
"createdBy": "DL",
"approved": true,
"reason": null,
"courseStatus": "APPROVED",
"courseCategory": {
"id": 1912,
"createdOn": 1572346929000,
"category": "Python Technology",
"description": "This Python For Beginners Course Teaches You The Python Language Fast. Includes Python Online Training With Python 3"
}
},
{
"id": 2043,
"createdOn": 1572604907000,
"name": "Learn Python Programming Masterclass",
"description": "This Python For Beginners Course Teaches You The Python Language Fast. Includes Python Online Training With Python 3",
"createdBy": "DL",
"approved": true,
"reason": null,
"courseStatus": "APPROVED",
"courseCategory": {
"id": 1912,
"createdOn": 1572346929000,
"category": "Python Technology",
"description": "This Python For Beginners Course Teaches You The Python Language Fast. Includes Python Online Training With Python 3"
}
},
{
"id": 2044,
"createdOn": 1572604961000,
"name": "The Python Bible™ | Everything You Need to Program in Python",
"description": "Build 11 Projects and go from Beginner to Pro in Python with the World's Most Fun Project-Based Python Course!",
"createdBy": "DL",
"approved": true,
"reason": null,
"courseStatus": "APPROVED",
"courseCategory": {
"id": 1912,
"createdOn": 1572346929000,
"category": "Python Technology",
"description": "This Python For Beginners Course Teaches You The Python Language Fast. Includes Python Online Training With Python 3"
}
},
{
"id": 2046,
"createdOn": 1572604968000,
"name": "The Python Bible™ | Everything You Need to Program in Python",
"description": "Build 11 Projects and go from Beginner to Pro in Python with the World's Most Fun Project-Based Python Course!",
"createdBy": "DL",
"approved": true,
"reason": null,
"courseStatus": "APPROVED",
"courseCategory": {
"id": 1912,
"createdOn": 1572346929000,
"category": "Python Technology",
"description": "This Python For Beginners Course Teaches You The Python Language Fast. Includes Python Online Training With Python 3"
}
},
{
"id": 2047,
"createdOn": 1572604969000,
"name": "The Python Bible™ | Everything You Need to Program in Python",
"description": "Build 11 Projects and go from Beginner to Pro in Python with the World's Most Fun Project-Based Python Course!",
"createdBy": "DL",
"approved": true,
"reason": null,
"courseStatus": "APPROVED",
"courseCategory": {
"id": 1912,
"createdOn": 1572346929000,
"category": "Python Technology",
"description": "This Python For Beginners Course Teaches You The Python Language Fast. Includes Python Online Training With Python 3"
}
},
{
"id": 2048,
"createdOn": 1572604971000,
"name": "The Python Bible™ | Everything You Need to Program in Python",
"description": "Build 11 Projects and go from Beginner to Pro in Python with the World's Most Fun Project-Based Python Course!",
"createdBy": "DL",
"approved": true,
"reason": null,
"courseStatus": "APPROVED",
"courseCategory": {
"id": 1912,
"createdOn": 1572346929000,
"category": "Python Technology",
"description": "This Python For Beginners Course Teaches You The Python Language Fast. Includes Python Online Training With Python 3"
}
},
{
"id": 2049,
"createdOn": 1572604972000,
"name": "The Python Bible™ | Everything You Need to Program in Python",
"description": "Build 11 Projects and go from Beginner to Pro in Python with the World's Most Fun Project-Based Python Course!",
"createdBy": "DL",
"approved": true,
"reason": null,
"courseStatus": "APPROVED",
"courseCategory": {
"id": 1912,
"createdOn": 1572346929000,
"category": "Python Technology",
"description": "This Python For Beginners Course Teaches You The Python Language Fast. Includes Python Online Training With Python 3"
}
}
],
"Mobile App Development": [
{
"id": 2002,
"createdOn": 1572528493000,
"name": "Flutter",
"description": "Flutter is a mobile technology",
"createdBy": "DL",
"approved": true,
"reason": null,
"courseStatus": "APPROVED",
"courseCategory": {
"id": 2001,
"createdOn": 1572528308000,
"category": "Mobile App Development",
"description": "Boom in market"
}
}
],
"Automation Testing": [
{
"id": 1950,
"createdOn": 1572435931000,
"name": "Selenium",
"description": "It's a automation testing tool for web",
"createdBy": "opp",
"approved": true,
"reason": "fvgbhnj",
"courseStatus": "APPROVED",
"courseCategory": {
"id": 1915,
"createdOn": 1572421405000,
"category": "Automation Testing",
"description": "\"TOP RATED (BEST SELLER) #1 Master SELENIUM java course\" -3 Million students learning worldWide with great collaboration"
}
},
{
"id": 2073,
"createdOn": 1572611314000,
"name": "string",
"description": "string",
"createdBy": "ski",
"approved": true,
"reason": null,
"courseStatus": "APPROVED",
"courseCategory": {
"id": 1915,
"createdOn": 1572421405000,
"category": "Automation Testing",
"description": "\"TOP RATED (BEST SELLER) #1 Master SELENIUM java course\" -3 Million students learning worldWide with great collaboration"
}
}
]
},
"message": "Success",
"timestamp": 1573199357155,
"status": 200,
"isSuccess": true
}
But here I'm facing an error while parsing the JSON Response
if let r = StudentTopCouses.init(JSON: data), let result = r.results {
for (key, value) in result {
print("Key: \( key )" )
print("Course Name: \( value.first!.name! )")
}
}
Error message:
Incorrect argument label in call (have 'JSON:', expected 'map:') Replace 'JSON' with 'map'
Upvotes: 0
Views: 2051
Reputation: 23495
You should read the documentation: https://github.com/tristanhimmelman/ObjectMapper
StudentTopCouses
constructor is init?(map: Map)
not init(JSON: ...)
So the error is correct on line: let r = StudentTopCouses.init(JSON: data)
where you don't have such a constructor..
The Mappable
protocol is as follows (as per link above in Mappable.swift):
public protocol Mappable: BaseMappable {
init?(map: Map)
}
As per documentation above, you'd have to do:
if let studentTopCourses = Mapper<StudentTopCourses>().map(JSONObject: SomeJSONDictionary)
or:
if let studentTopCourses = Mapper<StudentTopCourses>().map(JSONString: SomeJSONString)
EDIT: Since you are using SwiftyJSON
library AND ObjectMapper
library, you need to interface both of them:
if let jsonString = data.rawString(), let r = Mapper<StudentTopCouses>.map(JSONString: jsonString)
or:
//Can use JSON or JSONObject here..
if let jsonDict = data.dictionaryObject(), let r = Mapper<StudentTopCouses>.map(JSON: jsonDict)
Upvotes: 1