2017-05-09 1 views
0

私はAlamofireを使ってRiot APIを呼び出しており、呼び出された情報を表示したいと考えています。私は要求を働かせている、私はちょうどアプリケーションのラベルにリンクする方法を知らない。私はコードのスクリーンショットを含んでいます!APIデータをラベルに表示したい(Swift、Alamofire)

それは私が作成していますだけのシンプルなアプリです!

func callAlamo(url: String){ 
    Alamofire.request(url).responseJSON(completionHandler: { 
    response in 
    self.pasrseData(JSONData: response.data!) 
    }) 
    } 

func parseData(JSONData: Data){ 
    do { 
     var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as? JSONStandard 
     print(readableJSON) 
} 

catch { 
    print(error) 
    } 
} 
+1

コードを表示してください。 –

+0

編集したオリジナルのテキスト、コンソールレスポンスのスクリーンショットを開く –

答えて

0

は、基本的には、完成ブロックにラベルのテキストプロパティを設定します。

func callAlamo(url: String){ 
    Alamofire.request(url).responseJSON(completionHandler: { 
     response in 

     // here we say get me a non optional data (otherwise don't do the if) 
     if let data = response.data { 
      // here we are saying if you can't get me a value (i.e. not nil) for: 
      // json (note the try? will give nil if there is an error) 
      // name, we get the name out of the json dictionary 
      // then go to the else block, where we exit the function 
      // Happy case where we values for json and name we now have non optional variables W00t 
      guard 
      let json = try? self.parseData(JSONData: data), 
      let name = json["name"] as? String 
       else { 
        print("name does not exist in json: \(json)") 
        return 
      } 
      // Time to set the label 
      self.name.text = name 
     } 
    }) 
} 

// Changed this to return JSON as a dictionary (it looks like what you printed was a dictionary) 
// I also changed this so it throws the error and doesn't deal with it. 
// It probably doesn't know what to do if it can't read json something 
// else should handle the error higher up the stack 

func parseData(JSONData: Data) throws -> [String: Any]? { 
    return try JSONSerialization.jsonObject(with: 
    JSONData, options: .mutableContainers) as? [String: Any] 
} 

NB:あなたの持つ問題と私がテストしたソリューションのために行く場合、これは未テストです。

編集:別のプロパティを取得する方法を回答します。

私たちは、「名前」を得た方法は、このコードの塊だった:

guard 
let json = try? self.parseData(JSONData: data), 
let name = json["name"] as? String 
    else { 
     print("name does not exist in json: \(json)") 
     return 
} 

別のプロパティを取得するには、我々はこれを行うことができます:私たちは同じ操作を行うsummonerLevel表示するには、次に

guard 
let json = try? self.parseData(JSONData: data), 
let name = json["name"] as? String, 
let summonerLevel = json["summonerLevel"] as? Int 

    else { 
     print("name does not exist in json: \(json)") 
     return 
} 

を名前付き(ただし文字列ではありません)

// Time to set the label 
self.name.text = name 
// (you will have to create this new label) 
self.summonerLevelLabel.text = "Level: \(summonerLevel)" 
+0

これは治療法です!もう1つの質問は、 "名前"のようなjsonに別のエントリがありますが、それは "summonerLevel"です。どうすればそれを追加することもできますか? –

+0

また、Alamofireを使ってJSONをシリアライズするよりも良い方法があります。あなたが望むなら、私は例を送るでしょう。 –

0

以降はシリアル化する必要はありませんAlamofireのがそれを行っています。 JSONオブジェクトの内部に何が入っているのかわからないので、年齢と名前が返されたとしましょう:

struct InfoModel { // this struct will decompose our JSON object from the response that we get 
    var age:Int 
    var name:String 
    init(json:Dictionary<String,Any>?) { 
     guard let dict = json, 
     let age = dict["age"] as? Int, 
     let name = dict["name"] as? String 
      else {fatalError() } 
     self.age = age 
     self.name = name 

    } 
} 

func parse(url: String, completion:@escaping (InfoModel)-> Void) { 
    Alamofire.request(url).responseJSON {response in 
     // get the JSON dictionary 
     if let JSON = response.result.value { 
      // no need to decompose your object since your struct does it inside of its initializer 
      completion(InfoModel(json: JSON as? Dictionary<String, Any>)) 
     } 
    } 
} 
// call this function anywhere 
parse(url: "") { (m:InfoModel) in 
    print("age= \(m.age), name= \(m.name)") 
    // now populate your label 
    labelOne.text = "\(m.age)" 
    labelTwo.text = name 
} 
関連する問題