为什么 SwifyJSON dictionaryValue 是空的?
Why is SwifyJSON dictionaryValue empty?
我正在使用 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)
会导致 [:]
?
想通了,但我的解决方案与 SwiftyJSON(我仍然很好奇)无关。我更改了服务器通过套接字发送数据的方式。我将 socket.emit('hasBeenMatched', {user: JSON.stringify(currentUser)});
更改为 socket.emit('hasBeenMatched', {user: currentUser});
。本质上,我删除了手册 JSON-ification.
我正在使用 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)
会导致 [:]
?
想通了,但我的解决方案与 SwiftyJSON(我仍然很好奇)无关。我更改了服务器通过套接字发送数据的方式。我将 socket.emit('hasBeenMatched', {user: JSON.stringify(currentUser)});
更改为 socket.emit('hasBeenMatched', {user: currentUser});
。本质上,我删除了手册 JSON-ification.