Reputation: 3473
EDITED CODE
I had done in this way:
// Create new SBJSON parser object
SBJsonParser *parser = [[SBJsonParser alloc] init];
// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/maps?q=restaurant&sll=23.00,72.00&radius=2000&output=json"]];
// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSASCIIStringEncoding];
[json_string stringByReplacingOccurrencesOfString:@"while(1);" withString:@""];
NSDictionary *json_dict = [json_string JSONValue];
NSLog(@"%@",json_dict);
But then also I'm getting null
in json_dict
I want to parse the data which I get from Google Webservice.
http://www.google.com/maps?q=restaurant&sll=23.00,72.00&radius=2000&output=json
I have used the below code:
// Create new SBJSON parser object
SBJsonParser *parser = [[SBJsonParser alloc] init];
// Prepare URL request for Getting the Restaurent at particular coordinate.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/maps?q=restaurant&sll=23.00,72.00&radius=2000&output=json"]];
// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
// parse the JSON response into an object
// Here we're using NSArray since we're parsing an array of JSON status objects
NSArray *statuses = [parser objectWithString:json_string error:nil];
But I am not getting the response. If I use some other API like: http://twitter.com/statuses/public_timeline.json, it will parse properly, but this does not work for Google APIs
I had checked the response string in JSON version. The response which we get is forming a proper tree. It is not showing any error but the array in response which I get is null.
Upvotes: 0
Views: 2849
Reputation: 12405
EDIT:
I was looking into and found out that it is not a proper response as you get a file with JSON extension and not a JSON response as is the case with twitter api you are referring. So my advice refer google places api and find a JSON response for a list of restaurants and then try again.
In order to hit there api key you need authenticate with your own api key which you get using this.
once you get your api key you can hit the api like this..
this in return will give you a JSON response which you can parse easily (this returns a dictionary) and voila you are done(this api is limited to 100 hits per day only.)... hoping this helps.
So your code will look like this and will work I have checked it...
/// Create new SBJSON parser object
SBJsonParser *parser = [[SBJsonParser alloc] init];
// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=5000&types=food&sensor=false&key=yourOwnAPIKey"]];
// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
// parse the JSON response into an object
// Here we're using NSArray since we're parsing an array of JSON status objects
NSArray *statuses = [parser objectWithString:json_string error:nil];
NSLog(@"statuses:%@", statuses);
Upvotes: 3
Reputation: 6067
Try This One
Use SBJson
Instead of jsonValue
method.
1)import the "SBJSON.h"
in .h ViewController.
NSString *jsonString;
//jsonString this is The Coming JSON Response.
SBJSON *parser = [[[SBJSON alloc] init] autorelease];
NSDictionary *jsonResponse = (NSDictionary*)[parser objectWithString:jsonString error:nil];
NSDictionary *responseData = [jsonResponse objectForKey:@"response"]; objectForKey:@"venues"] ;
//Further parsed the Data According to the Keys.
Upvotes: 0
Reputation: 5098
Instead of creating a URLRequest you can directly download the contents from the URL into your jsonString like this
NSString * jsonString = [[NSString alloc] initWithContentsOfURL:@"http://www.google.com/maps?q=restaurant&sll=23.00,72.00&radius=2000&output=json" encoding:NSUTF8StringEncoding error:nil];
And convert this jsonString into NSDictionary using JSONParser.
Hope this will solve your issue.....
Upvotes: 0
Reputation: 11026
you can use google places api by replacing url to this
Generate your key from Google "https://code.google.com/apis/console/?pli=1#project:305239127220"
Upvotes: 1
Reputation: 4248
The response string from http://www.google.com/maps?q=restaurant&sll=23.00,72.00&radius=2000&output=json is started with while(1);
, it is not a valid JSON, remove it should work.
Upvotes: 1
Reputation: 11026
use this
NSDictionary *json_dict = [responseString JSONValue]; NSLog(@"%@",json_dict);
then you can fetch any value using its key
Upvotes: 2