Display json response in to UILabel

753 views Asked by At

My json Response is like this ..

{
sentdetails =     (
            {
        coursename = "Course Two";
        createddate = "05/12/2015 06:13 AM";
        currentprice = "2.00";
        "no_of_files" = 9;
        queryid = 36;
        querystatus = Open;
        submissionid = TSAb13d585e;
        testsaviornotes = None;
        title = Test;
        usernotes = rdgbv;
    }
);
status = Sucess;}

Now I am trying to display json data which is forth one namely "no_of_files" into UILabel and my code is like ..

NSMutableArray * aryy=[NSJSONSerialization JSONObjectWithData:responseObject  options:kNilOptions error:&error];
     jsonDisplay = [aryy valueForKey:@"sentdetails"];
     NSLog(@"%@",jsonDisplay);
     NSLog(@"\n\n\n%@",aryy);

     SentItemDetailViewController *viewController=[self.storyboard instantiateViewControllerWithIdentifier:@"SentItemDetailViewController"];

     viewController.numberofFiles = [[jsonDisplay objectAtIndex:0]valueForKey:@"no_of_files"];

But the value for "num_of_files" can't store on UILable I don't know what's the problem behind that.

4

There are 4 answers

1
Leo On BEST ANSWER
 NSNumber * num = [[jsonDisplay objectAtIndex:0]valueForKey:@"no_of_files"];
viewController.numberofFiles.text = [num stringValue];
3
Fabio Felici On

if numberofFiles is a UILabel you should write viewController.numberofFiles.text so:

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];
jsonDisplay = dict[@"sentdetails"];
NSLog(@"%@",jsonDisplay);
NSLog(@"\n\n\n%@",dict);
SentItemDetailViewController *viewController=[self.storyboard instantiateViewControllerWithIdentifier:@"SentItemDetailViewController"];
viewController.numberofFiles.text = [[jsonDisplay objectAtIndex:0][@"no_of_files"] stringValue];
3
sschunara On

Try

viewController.numberofFiles.text = [NSString stringWithFormat:@"%@",[[jsonDisplay objectAtIndex:0]valueForKey:@"no_of_files"]];

As I can notice that value for key no_of_files is not nsstring. Also need to give valuye to .text not label. :)

0
iOS_Tejas On

using Alamofire first you have to convert data(NSData) into string .

    var jsonString:String = ""
    var parseDataToShow:Data?
    @IBOutlet weak var lblToPrintDetails: UILabel!

 Alamofire.request(url, method: .post, parameters: parameter)
            .validate()
            .responseJSON(completionHandler: { (response) in

  debugPrint(response.result.value!)
// data form
  let jsonData1 = response.data 

   jsonString = String(data: parseDataToShow!, encoding: .utf8)!

    // you can print converted string data

    print("JsonString = \(jsonString)")
  self.lblToPrintDetails.text = jsonString

 })