I'm trying to post a http request and I have to deal with nested json.
I've tried to send my parameter as
[[String: Any]]
and
[JSON]
, but I get error in both situation.
this is my code:
var accountTitlesForNewProject = [JSON]()
for indexCounter in 0 ..< self.accountTitles.count {
let _accountTitle = self.accountTitles[indexCounter]
if !isChecked[indexCounter] {
continue
}
var _accountTitleJSON = JSON()
_accountTitleJSON["name"] = _accountTitle["name"]
_accountTitleJSON["description"] = _accountTitle["description"]
var _codes = [JSON]()
for (_, _code) in _accountTitle["accounting_codes"] {
let _codeJSON = JSON(dictionaryLiteral: ("type", _code["type"]), ("code", _code["code"]), ("level", _code["level"]))
_codes.append(_codeJSON)
}
_accountTitleJSON["accounting_codes"] = JSON(_codes)
accountTitlesForNewProject.append(_accountTitleJSON)
}
print(accountTitlesForNewProject)
self.addProject(projectName: projectName, stateId: stateId, cityId: cityId, accountTitles: accountTitlesForNewProject, successHandler: successHandler)
this is result of print:
[{"accounting_codes" : [
{"level" : 1,
"type" : 1,
"code" : "694"
},
{"level" : 2,
"type" : 1,
"code" : "312"
},
{"level" : 3,
"type" : 1,
"code" : "336"
}],
"name" : "بنفشه صفوی",
"description" : "لابد حرف و سخنی و خندهای و رفت. کنهای بود. درست یک پیرمرد. یک ساعت به ماهی سه چهار هفته بیشتر دوام نکرد.."
},
{"accounting_codes" : [
{"level" : 1,
"type" : 1,
"code" : "977"
},
{"level" : 2,
"type" : 1,
"code" : "568"
},
{"level" : 3,
"type" : 1,
"code" : "178"
}],
"name" : "آزاده میرزاده",
"description" : "و دیگه خسته شدهام. دلم میخواد قضیه به همین سادگی تمام میشود. و بعد چند سال سابقه دارد و چند نفری از اولیای."
}]
and this is addProject function where problem occurs:
private func addProject(projectName: String, stateId: Int, cityId: Int, accountTitles: [JSON], successHandler: @escaping (String) -> ()) {
let authenticator: TGAccessTokenProvider = TGOpenAuthentication.getTGOpenAuthenticator()
authenticator.getAccessToken(successHandler: { (_accessToken) in
self.projectProvider.request(.store(accessToken: _accessToken, projectName: projectName, stateId: stateId, cityId: cityId, accountTitles: accountTitles), completion: { ...
}
I got error when self.projectProvider.request executes and this is error message:
"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (_SwiftValue)'"
what's the problem?
as mentioned here, I solved my problem this way, cus my object property defined as:
private var accountTitles = [JSON]()
so this is the working code:
var accountTitlesForNewProject = [Any]()
for indexCounter in 0 ..< self.accountTitles.count {
let _accountTitle = self.accountTitles[indexCounter]
if !isChecked[indexCounter] {
continue
}
var _codes = [Any]()
for (_, _code) in _accountTitle["accounting_codes"] {
let _codeJSON: [String: Any] = ["type": _code["type"].intValue,
"code": _code["code"].stringValue,
"level": _code["level"].intValue]
_codes.append(_codeJSON)
}
let _accountTitleJSON: [String: Any] = ["name": _accountTitle["name"].stringValue,
"description": _accountTitle["description"].stringValue,
"accounting_codes": _codes]
accountTitlesForNewProject.append(_accountTitleJSON)
}