简体   繁体   English

如何在Swift 3.0中将JSON输出设置到UILabel中?

[英]How to set JSON output into UILabel in Swift 3.0 ?

I have these JSON data. 我有这些JSON数据。

(
        {
        email = "b@p.com.my";
        login = ID001;
        pw = 1234;
    },
        {
        email = "p@d.com.my";
        login = ID002;
        pw = 12345;
    }
)

Right now, I can only print myJSON value in x code output. 现在,我只能在x代码输出中打印myJSON值。

My question is, how to display each JSON into UILabel _email, _login, _pw? 我的问题是,如何将每个JSON显示到UILabel _email,_login,_pw?

Someone said that I need to store as variable and set into UILabel but i don't know how to do it. 有人说我需要将其存储为变量并设置为UILabel,但我不知道该怎么做。 Appreciate if someone can help me on this matters. 感谢有人可以帮助我解决这个问题。

ViewController.swift ViewController.swift

import UIKit
class ViewController: UIViewController {
    @IBOutlet var _email: UILabel!
    @IBOutlet var _login: UILabel!
    @IBOutlet var _pw: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        let url = URL(string: "http://localhost/get.php")
        let task = URLSession.shared.dataTask(with: url!) {
            (data, response, error) in
            if error != nil {
                print("Error")
                return
            }
            else {
                if let content = data {
                    do {
                        let myJSON = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                        print(myJSON)
                    }
                    catch {}   
                }
            }
        }
        task.resume()
    }
}

Thanks. 谢谢。

EDIT: After the block where you print your myJSON, try this: 编辑:在您打印myJSON的块之后,请尝试以下操作:

for eachItem in myJSON {
if let emailParsed = eachItem["email"] as? String {
    print(emailParsed)
    _email.text = emailParsed 
} 
}

This loop runs between all the dictionaries and This solution could be helpful for you. 此循环在所有词典之间运行, 解决方案可能对您有所帮助。 Please go through it! 请通过它!

Hi Please try this one 嗨,请尝试这个

   for i..0 < myJson.count { 
      let object = myJson[i] as AnyObject
      if let emailParsed = myJSON["email"] as? String {
         _email.text = emailParsed 
      }     
   }

EDITED 已编辑

for i..0 < myJson.count { 
      let object = myJson[i] as [String : AnyObject]
      if let emailParsed = myJSON["email"] as? String {
         _email.text = emailParsed 
      }     
   }

It's simple because your json object gives an array of dictionary so first you have to take first object from array and after that you can take the string passing the key 这很简单,因为您的json对象提供了一个字典数组,因此首先您必须从数组中获取第一个对象,然后您可以获取传递密钥的字符串

override func viewDidLoad() 
{
  super.viewDidLoad()
  let url = URL(string: "http://localhost/get.php")
  let task = URLSession.shared.dataTask(with: url!) 
             {
             (data, response, error) in
                  if error != nil
                     {
                       print("Error")
                       return
                     }
                  else 
                     {
                       if let content = data 
                          {
                             do {
                                  let myJSON = try JSONSerialization.jsonObject(with: content, options:JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                                 //   print(myJSON)
                                    for data in myJSON
                                    {
                                        let email = data.1["email"] as? String
                                        _email.text = email

                                    }
                                }
                                catch {}   
                          }
                      }
             }
             task.resume()
      }
}

For batter option you can use some predefined libraries like swiftyJSON. 对于连击选项,您可以使用一些预定义的库,例如swiftyJSON。 To get best result. 为了获得最佳结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM