Swift2 如果有时被检测为int而另一个被检测为string,如何转换swiftyjson值

Swift2 如果有时被检测为int而另一个被检测为string,如何转换swiftyjson值,swift2,alamofire,swifty-json,Swift2,Alamofire,Swifty Json,我使用alamofire和swiftyjson获取登录信息 Alamofire.request(.POST, postsEndpoint, parameters: newPost) .responseSwiftyJSON({ (request, response, json, error) in 在我的帖子回复中,我有这个价值 json["id_usuario"] 问题是,当值为-1或0(零)时,它可以作为int获得 使用 以及值为-1的响应示例 { "i

我使用alamofire和swiftyjson获取登录信息

Alamofire.request(.POST, postsEndpoint, parameters: newPost)
                .responseSwiftyJSON({ (request, response, json, error) in
在我的帖子回复中,我有这个价值

json["id_usuario"]
问题是,当值为-1或0(零)时,它可以作为int获得 使用

以及值为-1的响应示例

{
  "id_usuario" : -1
}
并且当响应值大于时,表示登录成功

{
  "estado" : "Jalisco",
  "plan_activo" : "0",
  "datos_registro_completos" : 1,
  "plan" : 0,
  "genero" : "H",
  "id_usuario_openpay" : "annvwl3didjylvex0wzh",
  "fb_id" : "10205386840402780",
  "email" : "steel.edward@hotmail.com",
  "postal_code" : "44630",
  "address" : "Nueva Escocia #1514 Interior 106",
  "nombres" : "Steel Edward",
  "app_mat" : "George",
  "app_pat" : "Vázquez",
  "ciudad" : "Guadalajara",
  "id_usuario" : "204",
  "admin" : "1",
  "phone_number" : "3334691505"
}
但如果值大于0,则返回nil,并且只能作为字符串获取

let idUser = json["id_usuario"].string
我的最终代码是这样工作的

if let idUser = json["id_usuario"].int {
   if(idUser == -1) {
        //specific error from the server
   } else if(idUser == 0) {
        //another specific error from the server                            
   }
} else if let idUser = json["id_usuario"].string {
          if(idUser != "") {
             //success
          }
}
if(idUser == -1) {
  //specific error from the server
} else if(idUser == 0) {
  //another specific error from the server
} else if (idUser > 0) {
  //success
}
我希望将值始终存储为Int,并使用它执行验证,并且有这样的代码

if let idUser = json["id_usuario"].int {
   if(idUser == -1) {
        //specific error from the server
   } else if(idUser == 0) {
        //another specific error from the server                            
   }
} else if let idUser = json["id_usuario"].string {
          if(idUser != "") {
             //success
          }
}
if(idUser == -1) {
  //specific error from the server
} else if(idUser == 0) {
  //another specific error from the server
} else if (idUser > 0) {
  //success
}

如果您拥有服务器代码,最好在那里找到一个bug……

这是我第一个不使用快速JSON的解决方案

    let id_usuario_object:NSObject = jsonData.valueForKey("id_usuario") as! NSObject

    var id_usuario:Int

    if ( id_usuario_object.isKindOfClass(NSString) ) {
         let id_usuario_string:NSString = jsonData.valueForKey("id_usuario") as! NSString
         id_usuario = Int(id_usuario_string.intValue)
    } else {
         id_usuario = jsonData.valueForKey("id_usuario") as! NSInteger
   }

问题出在PHP的JSON响应中

以前

echo json_encode( $results);
之后


在实际的JSON中,数据是如何传输的,因为这毫无意义?
但是如果值大于0,则返回一个nil,并且只能作为字符串获取,这没有多大意义。请向我们展示失败的大于0的值的示例。响应来自PHP,使用数组中的json_编码
echo json_encode( $results, JSON_NUMERIC_CHECK );