Parsing JSON for GOOGLE API Returning Images iPhone SDK
Just wondering if I am doing this correctly, It seems a lot of code for something so simple...
Passing the google json request with my key to this function using ASIHTTPREQUEST
NSString *url4Google = [[NSString alloc] initWithFormat:@"https://www.googleapis.com/shopping/search/v1/public/products?key=(MY SECRET KEY)&country=GB&q=5035822086234"];
ASIHTTPRequest* request = [self requestWithURL:url4Google];
Code for processing...
- (void)requestFinished:(ASIHTTPRequest*)request {
NSLog(@"GOOGLE SHOPPER IMAGE LOADING");
NSDictionary *result = [[request responseString] objectFromJSONString];
//GOOGLE SHOPPER IMAGE RETURN..
NSArray *Image = [[[[result objectForKey:@"items"] valueForKey:@"product"] valueForKey:@"images"] valueForKey:@"link"] ;
NSArray *Title = [[[result objectForKey:@"items"] valueForKey:@"product"] valueForKey:@"title"];
//NSLog(@"TITLE OF ITEM = %@", Title);
int size = [Image count];
NSLog(@"there are %d product images for this barcode ", size);
int titlez = [Title count];
NSLog(@"there are %d title names for this barcode ", titlez);
NSArray *imagearray = [Image objectAtIndex:3];
NSString *titleofitemis = [Title objectAtIndex:2];
//Choosing a number objectAtIndex...
NSMutableString * result1 = [[NSMutableString alloc] init];
for (NSObject * obj in imagearray)
{
[result1 appendString:[obj description]];
}
NSLog(@"FULL IMAGE URL IS = %@", result1);
//SET IMAGE
NSURL* _url = [[NSURL alloc] initWithString:result1];
self.url = _url;
[_url release];
[result1 release];
NSLog(@"url = %@", url);
UIImage* image = [ImageManager loadImage:url];
if (image) {
imageView.image = image;
}
//SET TITLE
self.title = titleofitemis;
NSLog(@"FULL TITLE NAME IS = %@", titleofitemis);
}
I am using SBJSON and also ImageManager to async load the image after it has located an image..
I have done a bit of memory management as for me that isn't too much of a problem for this example...
NSLOG OUTPUT
2011-09-28 16:29:17.351 IOSBoilerplate[17906:11c03] done pressed
2011-09-28 16:29:18.031 IOSBoilerplate[17906:11c03] GOOGLE SHOPPER IMAGE LOADING
2011-09-28 16:29:18.034 IOSBoilerplate[17906:11c03] there are 25 product images for this barcode
2011-09-28 16:29:18.035 IOSBoilerplate[17906:11c03] FULL URL FOR IMAGE = http://static.gam.co/media/films/00/000/0032/32467/image/30403.jpg
Is this the quickest way 开发者_StackOverflow中文版& has this been done right, I can do all error level detection later..
Any help would be appreciated.
Your code looks good. Some cleanup / style suggestions:
//...
NSMutableString *urlString = [NSMutableString string]; // shorter, autoreleased
for (NSString *s in imagearray) { // already typed
[urlString append:s];
}
self.url = [NSURL urlWithString:urlString]; // that's all it takes
UIImage *displayImage = [ImageManager loadImage:url];
// code is very readable, too
Please refrain from using instance names starting with a capital letter, such as your Image
or Title
. The convention is that capitalized names are used for classes, not instances. Also, more descriptive names such as imageArray
or imageList
makes the code much more understandable.
You can also use Google's Objective-C API library with the Shopping API. The library handles networking and authentication, and returns results as first-class Objective-C objects. The library includes a sample application for Shopping as well.
精彩评论