Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

ios - how to parse JSON array inside object in swift4

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()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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) }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...