2016-09-09 9 views
0

iOS開発者の新機能です。そして私はちょうどここに私を助けて、私はデータが適切なJSON形式で来ていないが、getメソッドを使用してAlamofireサードパーティのライブラリを添付する場合、キー値は、各identity..please のヌル来ることをお願いしたいと思います私のコードです:サードパーティライブラリのデータを適切なjson形式で使用しない

let headers = ["Authorization":"","Accept": "application/json"] 
Alamofire.request(.GET,requestString,headers:headers,encoding: .JSON) 
      .responseJSON { response in 
       print(response) 
       print(response.request) 
+0

私はresponse.result.value' 'で – Bansal

+0

ルックをサンプリングし、適切な型にキャスト提供してください。単純辞書の場合、 'let dictionary = response.result.value as if? [文字列:AnyObject] {...} '。または、辞書の配列、 'let dictionaries = response.result.value as if? [[String:AnyObject]] {...} '。その他 – Rob

+0

print(response.result.value)このコマンドを使用すると、データが{ addresses =( )になります。 apiKey = "66f36acb-8ec7-4945-9d64-d34557b1efa3"; baseCountry = ""; baseCurrency = ""; baseTimezone = ""; contactTypes = – Bansal

答えて

0

これを試してください。

Alamofire.request(.GET, url, parameters: parameter as? [String : AnyObject]).responseJSON { (response: Response<AnyObject, NSError>) in 
    if error == nil { 
     print(response.result.value) 
    } 
} 
+0

getメソッドではなぜパラメータが必要ですか? – Bansal

+0

パラメータなしでできますか? – Bansal

+0

もしあなたがいくつかのデータを渡したいのであれば、それをnsdictionaryとして渡してベースURLだけを渡すことができます。 &でそれを&。 –

0

responseJSONを呼び出すと、JSONが解析されます。したがって、JSONが(他の質問のいずれかで示されているように)辞書の配列であれば、response.result.valueには生のJSONではなく、配列と辞書の入れ子構造が含まれています。あなたは(例えばif letまたはguard letで)それをアンラップすることにより、このデータを取得することができます

Alamofire.request(requestString, headers: headers) 
    .responseJSON { response in 
     guard let dictionaries = response.result.value as? [[String: AnyObject]] else { 
      print(response.result.error) 
      return 
     } 

     // do something with the array of dictionaries, e.g. to show the names 

     for dictionary in dictionaries { 
      if let name = dictionary["name"] as? String { 
       print(name) 
      } else { 
       print("Name not found") 
      } 
     } 
} 
関連する問題