Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Loading JSON into IOS Application

Hi All,

I seem to be having trouble with loading the id value which is displayed within my iphone application.

When I run the app, it loads the the name, price and a and an image URL. The only section which fails to load depending on its value is the 'id'. Is anyone able to tell me why this happens just by looking at this code?

- (void) viewWillAppear:(BOOL)animated {
    NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://hostname/TM.php?id=%@",/*self.scannedValue */]]];
    //NSData *jsonData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"JSON"]];
    NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:Nil];
    NSDictionary *objectsDict = [jsonObject objectAtIndex:0];
    [self.nameLabel setText:[NSString stringWithFormat:@"Name: %@",[objectsDict objectForKey:@"name"]]];
    [self.priceLabel setText:[NSString stringWithFormat:@"Price: %@",[objectsDict objectForKey:@"price"]]];
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[objectsDict objectForKey:@"image_url"]]];
    [self.imageView  setImage:[UIImage imageWithData:imageData]];

Many Thanks.

Stone Preston
Stone Preston
42,016 Points

NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"",/*self.scannedValue */]]];

is that the line you are talking about? where do you set self.scannedValue at?

Stone Preston Yep, thats the line.

When I change %@ to an integer value it shows the image and other info for that id number. but I'm trying to get that part of the code to take any id number.

I set self scannedValue in my scannedViewController.m

Stone Preston
Stone Preston
42,016 Points

ok can you elaborate more on what you mean by "trying to get that part of the code to take any id number"

localhost/TM.php?id=101 <<-- displays data about one item localhost/TM.php?id=102 <<---- displays data about a different item localhost/TM.php?id=103 <<---- displays data about a different item etc..

I want to replace the '%@' with something that can take in any chosen id number. Just not too sure how to do that.

2 Answers


Stone Preston
Stone Preston
42,016 Points

have you tried using %d in place of %@? i havent worked much with JSON so i cant be of much help but if your JSON data for ID is an integer, then you probably need to use %d instead of %@. But I could be completely wrong haha.

I tried it but sadly it didnt work.

Its definetly something in this line which is wrong:

NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://localhost/TM.php?id=105",self.scannedValue]]];

I get a yellow message appear saying 'data argument not used by format string' underneath 'self.....

Not too sure what format I should be using in replacement.

Stone Preston
Stone Preston
42,016 Points

thats because you dont have a format specifier in your string, but pass in an argument to stringWithFormat

[NSString stringWithFormat:@"http://localhost/TM.php?id=105",self.scannedValue]

to fix it you would have to put a format specifier in place of 105

you need a format specifier in your string somewhere. Of course the type for specifier depends on the type of your scanned value property. What is the type of that property? can you post some code where you set that property to a value?

Hi Stone Preston

Sorry for the late reply.

I tried the following specifer before I posted in this forum a while ago but I couldnt get it to work:

[NSString stringWithFormat:@"http://localhost/TM.php?id="%@",self.scannedValue]

The property type for scannedValue is of NSString.

@property (nonatomic, retain) NSString *scannedValue;