-1

I'M using tableview to parsing JSON data. the parse data in tableview in successful parsed on my tableView but the problem is users click the tableview cell to pass to details ViewController.But the problem is i can't parse JSON in details ViewController in

here is my JSON looks

[
{
    "id": "263",
    "userId": "2692"
 }
 ]

here is my code

  guard let url = URL(string: URL API) else { return }
    var request = URLRequest(url: url)
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("Bearer \(AccessToken!)", forHTTPHeaderField: "Authorization")
    request.httpMethod = "GET"

    let session = URLSession.shared
    session.dataTask(with: request) { (data, response, error) in 
    do {
            let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [string: anyobject]

                print(json)
         label.text = json["id"] as? string

        }catch {


        }

 }.resume()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
ielyamani
  • 17,807
  • 10
  • 55
  • 90
  • 1
    `[string: anyobject]` (lowercased) doesn't compile. The JSON starts with a bracket (`[`) so it's clearly an array. Please read [Correctly Parsing JSON](https://stackoverflow.com/questions/39423367/correctly-parsing-json-in-swift-3/39423764#39423764) to learn the difference. – vadian Oct 22 '18 at 11:26
  • Where is your "didSelectCellAtIndexPath" method? You're only showing us the parsing, but you're telling us it errors when going to the detail view. Show us that code if that's the issue. If it is the parsing, are you parsing the same info twice? Can you not pass the object to your detail view instead? – TommyBs Oct 22 '18 at 11:26
  • in tableview i parse only one object that is ID in details view controller there are more data to parse soo i parse again in detail viewcontroller – user10155294 Oct 22 '18 at 11:29

2 Answers2

0

Please try this codes

do {
   if let json = try JSONSerialization.jsonObject(with: data!) as? [[String: String]] {
      for data in json {

          label.text  = data["id"] as? String
      }
   }
} catch { print(error) }
0

Parse json in swift4 using Codable protocol. declare your model like this:

struct Model: Codable {
    let id: Double
    let userId: Double

    enum CodingKeys : String, CodingKey {
        case id = "id"
        case userId = "userId"
    }
}

then, after getting data, use this:

do {
    let arrayValue = try JSONDecoder().decode([Model], from: data)
} 
catch {
}

Note that your json is array not dictionary!