【问题标题】:Parse JSON with Swift 3 XCode 8使用 Swift 3 XCode 8 解析 JSON
【发布时间】:2017-07-06 16:23:01
【问题描述】:

我有一个应用程序,允许您输入数据并将其发布到运行我创建的 mysql 的服务器。然后我编写了 PHP 文件以将 mysql 转换为 JSON,以便我的应用程序可以在表格中列出内容。但是它不起作用,我收到错误消息,提示“字符 0 周围的值无效”或“JSON 文本不是以数组或对象开头”。我要做的就是获取数据并将其转换为索引,以便我可以在整个应用程序中显示某些数据。

php代码:

//including the file dboperation
require_once '../includes/DbOperation2.php';

//creating a response array to store data
$response = array();

//creating a key in the response array to insert values
//this key will store an array iteself
$response['profile'] = array();

//creating object of class DbOperation
$db = new DbOperation();

//getting the teams using the function we created
$profile = $db->getAllUsers();

//looping through all the teams.
//creating a temporary array
$temp = array();

//inserting the team in the temporary array
$temp['id'] = $profiles['id'];
$temp['displayName']=$profiles['displayName'];
$temp['department']=$profiles['department'];
$temp['mamager']=$profiles['mamager'];
$temp['office']=$profiles['office'];
$temp['util']=$profiles['util'];

//inserting the temporary array inside response
array_push($response['profile'],$temp);
}

//displaying the array in json format
echo json_encode($response);

输出如下所示:

{"profile":[{"id":1,"displayName":"Jacob Blacksten","department":"DF","mamager":"San","office":"NYC","util":2},{"id":2,"displayName":"John Smith","department":"SS","mamager":"Jack","office":"Boston","util":3}]}

xcode:

   guard let url = URL(string: "http://ipAddress/MyWebService/api/getteams.php") else { return}

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

        if let data = data {
            print(data)
            do {
                let json = try JSONSerialization.jsonObject(with: data, options: [])
                print(json)
            } catch{
                print(error)
            }
        }
    }.resume()
}

请有人帮我处理 xcode。我需要先让它解析,一旦解析,我想把它们放在一个数组中并索引它们。我知道这可能是一个重复的问题,但我花了几天的时间寻找这个答案,但我对编码的了解不足以理解所有内容。

【问题讨论】:

  • 尝试比这更窄的标记。我根本看不到任何与 MySQL 相关的东西,而且 Xcode 也没有直接相关性。如果问题出在您的 PHP 代码中,请先验证该服务器端组件的输出是否正确,然后再让您的 Swift 代码正常工作。
  • 你说得对,MySQL 标签不正确。在上面你会看到服务器给我的输出。这正是我读过的所有文章的内容。对我来说,我在 xcode 中遗漏了一些东西,但我不知道是什么。
  • 这个错误是从哪里来的?当您调试 Swift 代码时,您是否在 data 中获得了正确的 JSON 字符串?
  • 错误是在“let json = try”行引发的,我根本看不到它应该传入的数据。

标签: php json swift


【解决方案1】:

有很多方法可以解析这个。我就是这样做的。

首先,创建一个NSObject 您将要解析的键的类。

class DataObject: NSObject { var Name: String? var ID: String?

}

在您的 ViewController 类中(或者任何您有检索数据的方法的地方),创建一个 DataObject 类型的全局变量

var dataObject = [DataObject]()

func retrieveData(dataToParse: [String : Any]) {

//dataToParse will be your json variable and cast it as a dictionary of [String : Any]

let object = DataObject()
object.setValuesForKey(dataToParse)
dataObject.append(object)

}

现在您可以像访问数组一样访问您的数据了。

let name = dataObject[0].Name

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多