Reputation: 183
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.
Upvotes: 1
Views: 756
Reputation: 269
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
})
Upvotes: 0
Reputation: 2285
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. :)
Upvotes: 2
Reputation: 24714
NSNumber * num = [[jsonDisplay objectAtIndex:0]valueForKey:@"no_of_files"];
viewController.numberofFiles.text = [num stringValue];
Upvotes: 2
Reputation: 2906
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];
Upvotes: 2