0

我正在使用 SwifyJSON 来解析通过 socket.io 发送到我的 iOS 应用程序的一些 JSON,.dictionaryValue并且响应是nil. 以下是从服务器发送数据的方式:

socket.emit('hasBeenMatched', {user: JSON.stringify(currentUser)});

这是我在 iOS 应用程序中的内容:

    socket.on("hasBeenMatched", callback: {data, ack in
        println("got response after requesting match");

        let user = JSON(data!)
        println(user)
        println(user[0])
        println(user[0]["user"])
        println(user[0]["user"].dictionaryValue)

    })

这是该代码的输出:

got response after requesting match
[
  {
    "user" : "{\"_id\":\"5511c3d8abcdc2fcf7b8fe4b\",\"email\":\"j\",\"password\":null,\"firstname\":\"j\",\"lastname\":\"j\",\"age\":9,\"radius\":\"9\",\"__v\":0,\"wantsToBeMatched\":true,\"matchedWith\":\"k k\"}"
  }
]
{
  "user" : "{\"_id\":\"5511c3d8abcdc2fcf7b8fe4b\",\"email\":\"j\",\"password\":null,\"firstname\":\"j\",\"lastname\":\"j\",\"age\":9,\"radius\":\"9\",\"__v\":0,\"wantsToBeMatched\":true,\"matchedWith\":\"k k\"}"
}
{"_id":"5511c3d8abcdc2fcf7b8fe4b","email":"j","password":null,"firstname":"j","lastname":"j","age":9,"radius":"9","__v":0,"wantsToBeMatched":true,"matchedWith":"k k"}
[:]

在我的代码的替代部分中,我有以下代码:

let request = Alamofire.request(.POST, "http://localhost:3000/api/users/authenticate", parameters: params)
request.validate()
request.response { [weak self] request, response, data, error in
    if let strongSelf = self {
        // Handle various error cases here....

        var serializationError: NSError?

        if let json: AnyObject = NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments, error: &serializationError) {
            println(JSON(json).dictionaryValue)

            // Story board navigation
        } else {
            //Handle error case
        }
    }
}

编辑:printlnAlamofire 响应处理中的输出如下所示:

[_id: 5511c3d8abcdc2fcf7b8fe4b, password: null, __v: 0, lastname: j, age: 9, wantsToBeMatched: true, firstname: j, radius: 9, email: j, matchedWith: k k]

我想知道的是:为什么会println(user[0]["user"].dictionaryValue)导致[:]

4

1 回答 1

0

想通了,但我的解决方案与 SwiftyJSON 无关(我仍然对此感到好奇)。我更改了服务器通过套接字发送数据的方式。我socket.emit('hasBeenMatched', {user: JSON.stringify(currentUser)});改为socket.emit('hasBeenMatched', {user: currentUser});. 本质上,我删除了手动 JSON 化。

于 2015-03-25T01:43:34.113 回答