2016-05-15 12 views
0

私は1つのプロジェクトの下にあります。その中で私はいくつかのデータを取得しており、その中で "_id"の値を取る必要があります。私の将来のuse.The以下のjsonの応答は私が取得しています。私は"_id" = 57383a6f74737692116174b9;を取る必要があります。そして私はこれを"_id"いくつかの変数に割り当てる必要があります。私はそれをラップし、変数に割り当てる方法はわかりません。jsonから値を取得し、変数として格納する方法

{ 
    "__v" = 0; 
    "_id" = 57383a6f74737692116174b9; 
    "business_id" =  { 
     "__v" = 0; 
     "_id" = 56cbf2547bb6427e2ac93cde; 
     "business_email" = "[email protected]"; 
     "business_name" = "State "; 
     "business_type" = 56cbed2e1c856c672a302ce4; 
     "created_at" = "2016-02-23T05:47:00.966Z"; 
     email = "[email protected]"; 
     gender = male; 
     images =   (
         { 
       url = "http://tor.jpg"; 
      }, 
         { 
       url = "http://tor.jpg"; 
      } 
     ); 
     "is_deleted" = 0; 
     languages =   (
      English, 
      Punjabi, 
      Hindi 
     ); 
     latlng =   (
      "3.6465059", 
      "-9.38014509999999" 
     ); 
     location = "200 Bay St, Torky"; 
     "method_payment" =   (
      Cash, 
      Visa, 
      Mastercard 
     ); 
     name = "Ms"; 
     "opening_hours" =   (
         { 
       Monday = "12:00pm - 09:00pm"; 
      }, 
         { 
       Tuesday = "12:00pm - 09:00pm"; 
      }, 
         { 
       Wednesday = "12:00pm - 09:00pm"; 
      }, 
         { 
       Thursday = "12:00pm - 09:00pm"; 
      } 
     ); 
     password = 12345; 
     "phone_no" = "+1 416"; 
     profileImageURL = "http://www.gospelherald.com/data/images/full.jpg"; 
     "social_links" =   { 
      "fb_url" = "https://facebook.com/sbi"; 
      "google_url" = "https://google.com/sbi"; 
      "twitter_url" = "https://twitter.com/sbi"; 
     }; 
     specialities =   (
      Cafe, 
      "Tea Room", 
      "Bubble Tea" 
     ); 
     status = 1; 
     "updated_at" = "2016-02-23T05:47:00.966Z"; 
     username = douglous; 
     videos =   (
         { 
       url = "https://www.youtube.com/watch?v=V87kFKgLOI0"; 
      }, 
         { 
       url = "https://www.youtube.com/watch?v=V87kFKgLOI0"; 
      } 
     ); 
     "website_url" = "url"; 
    }; 
    "created_at" = "2016-05-15T08:59:27.797Z"; 
    "customer_id" = 56fa77437e9cb5140ac510e2; 
    "is_deleted" = 0; 
    status = 1; 
    "updated_at" = "2016-05-15T08:59:27.797Z"; 
} 

私のビューコントローラでの私のコード:?私のDeleteFavで

var BusinessData = DeleteFav?() 

()私は1つの変数を持っている.AND私はその変数

func Getthedtafromfavouritegetmethod() { 

     let userToken = NSUserDefaults.standardUserDefaults().valueForKey("access_token") as! String; 

     let headers = [ 
      "x-access-token": userToken, 
      "cache-control": "no-cache", 
      "postman-token": "8eaa2c68-a459-21bf-569e-1e049d804be2" 
     ] 

     var request = NSMutableURLRequest(URL: NSURL(string: "some url")!, 
     cachePolicy: .UseProtocolCachePolicy, timeoutInterval: 10.0) 
     request.HTTPMethod = "GET" 
     request.allHTTPHeaderFields = headers 

     let session = NSURLSession.sharedSession() 
     let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in 
      if (error != nil) 
      { 
       print(error) 

       let ErrorAlert = UIAlertController(title: "Error", message: "Problem with internet connectivity or server, please try after some time", preferredStyle: UIAlertControllerStyle.Alert) 

       // add an action (button) 
       ErrorAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 

       // show the alert 
       self.presentViewController(ErrorAlert, animated: true, completion: nil) 
      } 
      else 
      { 
       if let json = (try? NSJSONSerialization.JSONObjectWithData(data!, options: [])) as? Dictionary<String,AnyObject> 
       { 
        let success = json["success"] as? Int 

        if(success == 1) 
        { 

         if let typeValues = json["data"] as? [NSDictionary] 
         { 
          dispatch_async(dispatch_get_main_queue(),{ 

           for item in typeValues 
           { 

            print(item) 


           } 


          }) 
         } 
        } 
        else 
        { 
         let message = json["message"] as? String 

         let ServerAlert = UIAlertController(title: "Error", message: message, preferredStyle: UIAlertControllerStyle.Alert) 

         // add an action (button) 
         ServerAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 

         // show the alert 
         self.presentViewController(ServerAlert, animated: true, completion: nil) 
        } 
       } 
      } 
     }) 

     dataTask.resume() 


    } 
に私の "_id" を割り当てる必要があります

私は上記の完全なコードの下の行のコードから正しい応答を得ているかどうか確認します:

for item in typeValues 
{ 
print(item) 
} 

どのように私のカスタムクラスの変数に "_id"を割り当てることができますか?私は新しいiosです。そして、いくつかのyoutube videos.Anyの助けが私のために役立つでしょうありがとう... !!

更新日:

を、私は1つのカスタムクラスを持っている:

import UIKit 

class DeleteFav: NSObject { 

    var FavId : String? 


    init(json:NSDictionary) 
    { 
     self.FavId = json["_id"] as? String 

    } 

} 

答えて

0

あなたがでライブラリを試すことができます:https://github.com/ibireme/YYModel

// Convert json to model: 
User *user = [User yy_modelWithJSON:json]; 

// Convert model to json: 
NSDictionary *json = [user yy_modelToJSONObject];` 
+0

本当に私はios.Iに新しいです方法を知ってhaventはこのライブラリfiles.Isを処理するために、他のアイデア??? – mark

+0

私はこのライブラリをダウンロードして、私は私のproject.Now ahtを追加する必要がありますか? – mark

+0

プロジェクトにダウンロードしてドラッグするだけで、 '#import"を追加します。YYModel.h "' –