Reputation: 11
I would like to convert my json response to an array but it doesn't work and I got an error that says my items in the array are nil
func getcomments(){
RestApiManager.sharedInstance.getComments(TUTORIAL_ID: id){
response in
let comments = JSON(response)
for item in comments.array!{
let comment = Comment(memail: String(describing: item["email"]), mcomment: String(describing: item["comment"]), mcomment_date: String(describing: item["comment_date"]), manswer: String(describing: item["answer"]), manswer_date: String(describing: item["answer_date"]))
self.comments.append(comment)
}
}
}
it is my json response:
[{
"email": "-",
"comment": "\u0627\u0632 \u0627\u067e\u0644\u06cc\u06a9\u06cc\u0634\u0646 \u062e\u0648\u0628\u062a\u0648\u0646 \u0645\u0645\u0646\u0648\u0646\u0645",
"comment_date": "2017-07-15 19:30:00",
"answer": null,
"answer_date": null
},
{
"email": "[email protected]",
"comment": "salam",
"comment_date": "2017-07-11 19:30:00",
"answer": "\u062a\u0634\u06a9\u0631",
"answer_date": "2017-07-12 03:50:57"
}
]
I got nil error in this line:
unexpectedly found nil while unwrapping an Optional value
for item in comments.array!
Upvotes: 0
Views: 1773
Reputation: 271555
According to your comment, response
is actually a string. Therefore, you can't just create a JSON using init(_:)
. You need init(parseJSON:)
.
init(_:)
will just create a JSON with just that string instead of a JSON object, which is obviously not what you want. init(parseJSON:)
will actually parse your JSON string and allow you to access the different key value pairs.
func getcomments(){
RestApiManager.sharedInstance.getComments(TUTORIAL_ID: id){
response in
let comments = JSON(parseJSON: response)
Upvotes: 1
Reputation: 139
It's easier if you decode it as an array of structures.
First, create the struct:
struct Comment: Codable {
var email: String
var comment: String
var comment_date: String
var answer: String
var answer_date: String
}
Then you can just call the JSON like that:
guard let url = Bundle.main.url(forResource: resource, withExtension: "json") else {
throw Errors.couldNotFindResource
}
data = try! JSONDecoder().decode([Comment].self, from: Data(contentsOf: url))
Upvotes: 1