2016-07-14 2 views
-2

現在、私のウェブアプリケーションにdjangoバックエンドを使用するモバイルアプリケーションを作成中です。即時でjsonデータを読む

は、私はその後、私は戻って、この応答を取得迅速でサーバーに要求を作る

[{"model": "webapp.comment", "pk": 73, "fields": {"commentDescription": "hello there", "owner": 25, "postId": 78}}, 

{"model": "webapp.comment", "pk": 72, "fields": {"commentDescription": "well hi", "owner": 25, "postId": 78}}] 

私は、ループを通って、commentDescriptionownerpkのように、これらの値にアクセスし、使用して自分のモバイルアプリケーションでそれらを使用するにはどうすればよいです文字列として迅速に。私はグロスそれを読むために使用しようとしましたが、私はまだ失われています。

答えて

0

あなたはこの

guard let stringData = JSONSTRING.dataUsingEncoding(NSUTF8StringEncoding, 
      allowLossyConversion: false) else { return } 

     do { 
      if let dict = try NSJSONSerialization.JSONObjectWithData(stringData, 
       options: NSJSONReadingOptions.MutableContainers) 
} catch { 
      logAndError("Got unknown error from server") 
     } 
2

SwiftyJSONライブラリを使用してみてください

import SwiftyJSON 

let jsonData = JSON(data:data) // data is your NSData JSON response 
for (_,item):(String, JSON) in jsonData { //loop through your json objects 
    print(item["model"].stringValue) 
    print(item["pk"].intValue) 
    print(item["fields"][commentDescription].stringValue) 
    print(item["fields"]["owner"].stringValue) 
    print(item["fields"]["postId"].intValue) 
}