2016-08-23 74 views
-1

嗨,我有一個應用程序,其中有一個UITableView顯示了json的結果。我需要幫助在單元格上顯示json對象。這是JSON的結果:xcode解析json表格視圖

{ 
followers =  (
      { 
     "_id" = 
     timestamp = 
     user =    { 
      "__v" = 0; 
      "_id" = 
      birthdate = 
      coverImageURL = "<null>"; 
      "f_enabled" = 1; 
      first = 
      followStatus = 0; 
      followers = 0; 
      following = 1; 
      id = 
      idDatabase = 8; 
      lang = it; 
      last = 
      profileImageURL = 
      second = "<null>"; 
      username = 
      views = 10; 
     }; 
    }, 
      { 
     "_id" = 
     timestamp = 
     user =    { 
      "__v" = 0; 
      "_id" = 
      birthdate = "<null>"; 
      coverImageURL = "<null>"; 
      "f_enabled" = 1; 
      first = "<null>"; 
      followStatus = 0; 
      followers = 0; 
      following = 2; 
      id = 
      idDatabase = 
      lang = "it-IT"; 
      last = "<null>"; 
      profileImageURL = "<null>"; 
      second = "<null>"; 
      username = 
      views = 0; 
     }; 
    }, 
      { 
     "_id" = 
     timestamp = 
     user =    { 
      "__v" = 0; 
      "_id" = 
      birthdate = 
      coverImageURL = 
      "f_enabled" = 1; 
      first = Mark; 
      followStatus = 0; 
      followers = 0; 
      following = 8; 
      id = 
      idDatabase = 72; 
      lang = it; 
      last = Fat; 
      profileImageURL = 
      second = "<null>"; 
      thumbImageURL = 
      username = 
      views = 10; 
     }; 
    }, 
      { 
     "_id" = 
     timestamp = 
     user =    { 
      "__v" = 0; 
      "_id" = 
      birthdate = "<null>"; 
      coverImageURL = 
      "f_enabled" = 1; 
      first = "<null>"; 
      followStatus = 0; 
      followers = 0; 
      following = 6; 
      id = 
      idDatabase = 
      lang = it; 
      last = "<null>"; 
      profileImageURL = 
      second = "<null>"; 
      thumbImageURL = 
      username = 
      views = 0; 
     }; 
    }, 
      { 
     "_id" = 
     timestamp = 
     user =    { 
      "__v" = 0; 
      "_id" = 
      birthdate = 
      coverImageURL = 
      "f_enabled" = 1; 
      first = 
      followStatus = 0; 
      followers = 0; 
      following = 6; 
      id = 
      idDatabase = 
      lang = it; 
      last = 
      profileImageURL = 
      second = "<null>"; 
      thumbImageURL = 
      username = 
      views = 0; 
     }; 
    }, 
      { 
     "_id" = 
     timestamp = 
     user =    { 
      "__v" = 0; 
      "_id" = 
      birthdate = 
      coverImageURL = 
      "f_enabled" = 1; 
      first = 
      followStatus = 0; 
      followers = 4; 
      following = 11; 
      id = 
      idDatabase = 29; 
      lang = "it-IT"; 
      last = 
      profileImageURL = 
      second = "<null>"; 
      username = 
      views = 44; 
     }; 
    } 
); 

}

,這是我用它來解析JSON的FUNC:

func loadData() 
{ 
    let url = NSURL(string: "") 
    let request = NSMutableURLRequest(URL: url!) 
    request.HTTPMethod = "POST" 
    request.addValue("application/json", forHTTPHeaderField: "Content-Type") 
    request.addValue("Bearer \(AppPreferences.getAuthToken())", forHTTPHeaderField: "Authorization") 

    let dm = DataManager(); 
    dm.initTotalStringReq(); 

    dm.addParamStringReq("lang", value: "it-IT",comma: true) 
    dm.addParamStringReq("id", value:"",comma: false) 

    dm.finishTotalStringReq() 
    print(dm) 

    request.HTTPBody = dm.getTotalStringReq().dataUsingEncoding(NSUTF8StringEncoding); 

    let session = NSURLSession.sharedSession() 
    let task = session.dataTaskWithRequest(request) { data, response, error in 
     if let response = response, data = data { 
      print(response) 
      let dataString = "[" + String(data: data, encoding: NSUTF8StringEncoding)! + "]"; 
      let nsdata:NSData = dataString.dataUsingEncoding(NSUTF8StringEncoding)! 
      self.item = DataManager.parseJSON(nsdata); 
      print(self.item); 

      dispatch_async(dispatch_get_main_queue(), { 
       self.tableView.reloadData() 
      }) 
     } else { 
      print(error) 
     } 
    } 

    task.resume() 

} 
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 

    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 



    if ((self.item) != nil) 
    { 

     return (self.item[0]["result"]!!["followers"] as? NSArray)!.count; 
    } 
    else 
    { 
     return 0; 
    } 

    // return item.count 

} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("followerCell", forIndexPath: indexPath) as? FollowerTableViewCell 



    let row = indexPath.row; 


    cell!.nameFollower.text = (self.item[0]["result"]!!["followers"] as! NSArray)[row]["user"] as? String; 
    print("2") 

    //UtilityFunc.loadImages((cell!.imgProfile)!, url: ((self.dataLikes[0]["result"]!!["likes"] as! NSArray)[row]["profileImageURL"] as? String)!); 


    return cell! 
} 

我需要顯示 「用戶」 NSDictionary裏面的對象。

回答

0

試試這個

let task = session.dataTaskWithRequest(request) { data, response, error in 
      if let response = response, data = data { 
       print(response) 
       let dataString = "[" + String(data: data, encoding: NSUTF8StringEncoding)! + "]"; 
       let nsdata:NSData = dataString.dataUsingEncoding(NSUTF8StringEncoding)! 
if let arrTemp = DataManager.parseJSON(nsdata).valueforKey("followers") as? NSArray{ 
       self.item = arrTemp; 
       print(self.item); 

       dispatch_async(dispatch_get_main_queue(), { 
        self.tableView.reloadData() 
       }) 
} 
      } else { 
       print(error) 
      } 
     } 

細胞用於行

if let strBithdate = self.item.objectAtIndex[indexPathRow].valueForKey("birthdate") as? string 
{ 
lblbirthdate = strBithdate 
} 
+0

但我需要的用戶詞典中的對象! –

+0

你可以指定哪種對象? –

+0

我使用的代碼工作,因爲顯示計數正確,但在單元格中,我需要顯示「用戶」nsdictionary內的對象。如果您看到json是Nsdictionary追隨者Nsarray和用戶Nsdictionary的結果。 –