How to append the data from the JSON


How to append the data from the JSON



How to append the data ?



my api as:-


[
{
"Name": "Vegetables",
"PictureModel": {
"ImageUrl": "http://example.jpeg"
}
},
{
"Name": "Fruits",
"PictureModel": {
"ImageUrl": "http://example1.jpeg"
}
}
]



for fetching the data from json


let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}

if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: ) as! Array<[String:Any]>
print(json)


let type = json[0]
let name = type["Name"] as! String
let PictureModel = type["PictureModel"] as! [String:Any]
print(PictureModel)

let nameofimage = PictureModel["ImageUrl"] as! String
print(nameofimage)

self.fetchedHome.append(Categories(name: name, images: nameofimage))
self.tableview.reloadData()


} catch {
print(error)
}
}

}.resume()



And i got the output in the tableview .In tableview i got first data as name-vegetable and image in UIImageview.



.But second data not display .So how to append ? how to give for loop and append




3 Answers
3



Use this code you will get one by one object in For loop:-


if let data = json as? NSArray {
for data in data {
if let data = data as? [String: AnyObject] {
//Here you will get data
print("Name", data["Name"])
}
}
}



Are you getting JSON correctly in print(json)?



If yes, then you should try looping on "json" as follows :-


for (var keyAttr in json) {
let dataRow = json[keyAttr]
}



Inside the dataRow you can get "Name" and "PictureModel".



I am hoping that it should work.



All the best.


let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}

if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: ) as! Array<[String:Any]>
if let data = json as? [String:Any] {
for data in data {

let name = data["Name"] as! String
let PictureModel = data["PictureModel"] as! [String:Any]
print(PictureModel)

let nameofimage = PictureModel["ImageUrl"] as! String
print(nameofimage)

self.fetchedHome.append(Categories(name: name, images: nameofimage))
}
}
self.tableview.reloadData()


} catch {
print(error)
}
}

}.resume()





please check i have updted my code in that how to do?
– brucecraigb
2 days ago





@brucecraigb have you implement any answer in your code
– IOS
2 days ago





i got the answer vegetable but second data is not appended ,so to solve it?
– brucecraigb
2 days ago





replace my code to your code and check
– IOS
2 days ago





here showing an error s :-conditional cast from Array<[String:Any]> to NSArray in the code of:- if let data1 = json as? NSArray {......how to solve it
– brucecraigb
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Possible Unhandled Promise Rejection (id: 0): ReferenceError: user is not defined ReferenceError: user is not defined

Opening a url is failing in Swift