2017-05-08 9 views
0

私はそうと私のサーバーから来てJSONオブジェクトを持っている:解析スウィフト辞書の二次レベル

LOGIN_SUCCESS with JSON: { 
    token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJteS5kb21haW4uY29tIiwic3ViIjoiNTkwOTRkNjRjMmRhN2E3MWI4NTljYTFhIiwiaWF0IjoxNDk0MjY1NTE1LCJleHAiOjE0OTQ4NzAzMTV9.SqsLeToG8-_3CV1Yr4Z4SUIv4-vqGbntGwFLB4i7n-w"; 
    user =  { 
     "__v" = 0; 
     "_id" = 59094d64c2da7a71b859ca1a; 
     createdAt = "2017-05-03T03:24:20.309Z"; 
     email = "[email protected]"; 
     name = Dylan; 
     updatedAt = "2017-05-03T03:24:20.309Z"; 
    }; 
} 

これはUSERINFOとして辞書に変換します。しかし、私のトークンはuserinfoに返されないので、これをどのように解析してクラスにすることができます。現在、私の 初期化は、ハードコードされた文字列で次のようになります。

self.loggedInUser.setUser(firstName: "Dylan" as String!, 
          email: "[email protected]"as String!, 
          token: "test" as String!, 
          id: "1"as String!, 
          longitude: "40.0"as String!, 
          latitude: "-70"as String!) 

self.loggedInUser.printUser() 

全リクエスト試み:

func loginPost(email: String, password: String, completion: @escaping (_ userInfo: User?, _ error: [[String : Any]]?) -> Void) { 
     let headers: HTTPHeaders = ["Content-Type" : "application/json"] 
     let parameters: Parameters = ["email": "\(email)","password": "\(password)"] 

Alamofire.request(loginURL, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers) 
    .validate(contentType: ["application/json"]) 
    .responseJSON { response in 
     if response.response?.statusCode == 200 { 
      print("LOGIN_SUCCESS with JSON: \(response.result.value!)") 
      if let userInfo = response.value as? [String : Any] { 


       self.loggedInUser.setUser(firstName: userInfo.["user"]["name"] as! String!, 
              email: userInfo["email"] as! String!, 
              token: userInfo["token"] as! String!, 
              id: "1"as String!, 
              longitude: "40.0"as String!, 
              latitude: "-70"as String!) 
       self.loggedInUser.printUser() 
       return completion(self.loggedInUser, nil) 
      } 
     } else { 
      print("LOGIN_FAILURE with JSON: \(response.result.value!)") 

      if let error = response.result.value as? [[String: Any]] { 
       //If you want array of task id you can try like 
       return completion(nil, error) 
      } 
     } 
} 

}

+0

ください[ObjectMapper](https://github.com/Hearst-DD/ObjectMapper)フレームワークを見てください –

+0

@ CodeDefferentありがとうございます。 – user1093111

答えて

1

は、以下の修正コードを探す:

if response.response?.statusCode == 200 { 
      print("LOGIN_SUCCESS with JSON: \(response.result.value!)") 
      if let responseData = response.value as? [String : Any] { 
       let token = responseData["token"] as? String 
       if let userInfo = responseData as? [String : Any] { 
       self.loggedInUser.setUser(firstName: userInfo.["user"]["name"] as! String!, 
              email: userInfo["email"] as! String!, 
              token: userInfo["token"] as! String!, 
              id: "1"as String!, 
              longitude: "40.0"as String!, 
              latitude: "-70"as String!) 
       self.loggedInUser.printUser() 
       return completion(self.loggedInUser, nil) 
      } 
      } 
     } 
+0

よろしくお願いします。最初にトークンを取り出してください。 2Dのユーザーデータにアクセスするにはどうすればよいですか?例:userInfo。["user"] ["name"] – user1093111

+0

「タイプ」に「下付きメンバがありません」というエラーが表示される – user1093111

+0

レイヤーをレイヤーごとに解析する必要があります。サードパーティのフレームワーク。 最初に 'user = userInfo [" user "]としますか? [String:Any] 'を実行してから' let name = user ["name"]を? String' –