ios - JSON structure with Swift and Alamofire -
following on this question
i trying bring in summary field has further value. have brought in title , author follows:
func parsedata(jsondata : data) { { var readablejson = try jsonserialization.jsonobject(with: jsondata, options: .mutablecontainers) as! jsonstandard // print(readablejson) if let posts = readablejson["posts"] as? [jsonstandard] { post in posts { let title = post["title"] as! string let author = post["author"] as! string let summary = post["summary"] as! string print(author) if let imageurl = post["image"] as? string { let mainimageurl = url(string: imageurl ) let mainimagedata = nsdata(contentsof: mainimageurl!) let mainimage = uiimage(data: mainimagedata as! data) postsinput.append(postinput.init(mainimage: mainimage, name: title, author: author, summary: summary)) } } dispatchqueue.main.async { self.tableview.reloaddata() } } } catch { print(error) } }
but if try summary way error returned. appreciated.
json structure
{ "posts" : [{ "id": "000000", "url": "/content/interview2", "date": "2016-11-03 09:01:41", "modified": "2016-11-03 09:03:47", "title": "an interview", "image": "https://www.example.com/sites/default/files/oregood.jpeg", "summary": { "value": "<p>latin text here</p>", "format": "filtered_html" } }] }
from previous question response, summary
key contains dictionary
can try this.
guard let dic = post["summary"] as? [string: any], let summary = dic["value"] as? string else { return }
Comments
Post a Comment