2015-03-25 15 views
0

我正在使用SwifyJSON解析通過socket.io發送到我的iOS應用的一些JSON,並且.dictionaryValue的響應是nil。下面是如何將數據從服務器發送:爲什麼SwifyJSON dictionaryValue爲空?

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 
     } 
    } 
} 

編輯:println在Alamofire響應處理輸出的樣子:

[_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

想通了,但我的解決方案是不相關的SwiftyJSON(我仍然很好奇)。我改變了我的服務器通過socket發送數據的方式。我將socket.emit('hasBeenMatched', {user: JSON.stringify(currentUser)});更改爲socket.emit('hasBeenMatched', {user: currentUser});。本質上,我刪除了手動JSON化。