我使用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
}
}
}
编辑:Alamofire响应处理中println
的输出如下:
[_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)
导致[:]
?
答案 0 :(得分:0)
想出来,但我的解决方案与SwiftyJSON(我仍然很好奇)有关。我改变了服务器通过socket发送数据的方式。我将socket.emit('hasBeenMatched', {user: JSON.stringify(currentUser)});
更改为socket.emit('hasBeenMatched', {user: currentUser});
。基本上,我删除了手动JSON-ification。