I am building an app that makes use of google places api,i use this url
the desired link doesnt produce any results when i use it in the browser
and i use json to parse the data coming from google places but i get this unusual warning which says NSString may not respond to JSONValue
the code is as follows
-(IBAction)nearbyLocations:(id)sender
{
NSString *url=[NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/json?location=37.329558,122.025002&radius=500&types=atm&sensor=false&key=AIzaSyCIZ8MxCoMsfgj0ytE7azXGfjs_E__2Nhw"];
NSURL *googleRequestURL=[NSURL URLWithString:url];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
[self performSelectorOnMainThread:#selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
-(void)fetchedData:(NSData *)responseData
{
//parse out the json data
//NSError* error;
NSString *jsonString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"%#",jsonString);
NSDictionary* json =[jsonString JSONValue];
//[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
//The results from Google will be an array obtained from the NSDictionary object with the key "results".
NSArray* places = [json objectForKey:#"results"];
//Write out the data to the console.
NSLog(#"Google Data: %#", places);
}
You need a larger radius. In the URL you provided, I changed radius=500 to radius=5000 and it works. New URL. Basically there are no ATMs within 500 meters of that lat/long, but there are ATMs within 5000 meters.
That's one issue. The other issue is in Tsar's comment to your question.
Related
i want to take 450
from the following:
NSString {status:0,val:450}
using objective c:
{status:0,val:450}
Please suggest me answer
I cannot completely understand your question . I think you have you have an JSON data like:
{
status:0;
val:450;
}
If you want this data in your app. You want to do
NSURL *blogURL = [NSURL URLWithString:#"https://your url here"];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSDictionary *data = [dataDictionary objectForKey:#"status"];
NSDictionary *item = [data objectForKey:#"val"];
Your JSON Data get in Dictionary format. You can access the Data using the Key. In here your keys are status and val.
I hope this links are help for you.
fetch parse json ios programming tutorial
and this Link:
json parsing in ios
I am retriving some file from the web containing data in a specific format which I would like to parse. However I know only how to get the file from the web:
dispatch_async(server_queue, ^{
NSData* data = [NSData dataWithContentsOfURL:
kURL];
[self performSelectorOnMainThread:#selector(parseData:)
withObject:data waitUntilDone:YES];
});
In the parse method I would like to tokenize each line of the file but I am not sure how to extract the lines from the NSData object.
-(void)parseData:(NSData *)responseData {
//tokenize each line of responseData
}
Any suggestion?
NSData is not in a format where you can go through to parse it. Just convert it to a NSString like this:
-(void)parseData:(NSData *)responseData
{
NSString *stringFromData = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSArray *eachLineOfString = [stringFromData componentsSeparatedByString:#"\n"];
for (NSString *line in eachLineOfString) {
// DO SOMETHING WITH THIS LINE
}
}
I am new to iOS and working on the GOOGLE-MAP-SDK. Everything is going very well, but I am not able to use the annotations in this case due to which I am not able to locate my positions which I fetched from the placeAPI of Google.
So kindly help me out with my errors.
Code File
-(IBAction)search:(id)sender
{
NSString *url = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/json?location=30.7343000,76.7933000&radius=500&types=food&name&sensor=true&key=AIzaSyCGeIN7gCxU8baq3e5eL0DU3_JHeWyKzic"];
//Formulate the string as URL object.
NSURL *googleRequestURL=[NSURL URLWithString:url];
// Retrieve the results of the URL.
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: googleRequestURL];
[self performSelectorOnMainThread:#selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
//The results from Google will be an array obtained from the NSDictionary object with the key "results".
NSArray* places = [json objectForKey:#"results"];
//Write out the data to the console.
NSLog(#"Google Data: %#", places);
}
Here is the code from which I will receive the data and want to display it on the Google map. NSLog(#"Google Data: %#", places); is giving me the out put...
I am looking to use JSON data to create a route using the Google Maps API and extract JSON data that will be displayed onto a text field that includes "distance" and "duration". I would like to have to text fields that will reverse geocode and send a request to the Google Map API. Here is an example code I'm using:
// Create new SBJSON parser object
SBJSON *parser = [[SBJSON alloc] init];
// Prepare URL request to download statuses from Twitter
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://twitter.com/statuses/public_timeline.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];
// Each element in statuses is a single status
// represented as a NSDictionary
for (NSDictionary *status in statuses) {
// You can retrieve individual values using objectForKey on the status NSDictionary
// This will print the tweet and username to the console
NSLog(#"%# - %#", [status objectForKey:#"text"], [[status objectForKey:#"user"] objectForKey:#"screen_name"]);
}
Rather than it print onto the screen I would like for this to print onto a text field while gathering specific data from the JSON data. I would also like to be able to reverse geocode.
quite new to iOS development and objective-c at the same time. I have the following method:
-(NSMutableArray *)fetchDatabaseJSON{
NSURL *url = [NSURL URLWithString:#"http://www.ios.com/ios/responseScript.php"];
NSError *error = nil;
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:&error];
//jsonArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
//NSLog(#"Array: %#",[jsonArray objectAtIndex:0]);
jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(#"Dictionary: %#", jsonDictionary);
return jsonArray;
}
Now the NSLog shows this:
2013-02-03 19:15:37.081 TestConnection[24510:c07] Dictionary: (
Bannana,
Apple,
SomeCheese )
From what I understand that whatever is inside the dictionary doesn't have key-value. How can this be? and how can I fix it? I want to be able to have keys to ease operations on dictionary.
Regards,
JSONObjectWithData may return an NSArray or NSDictionary, depending on the JSON data you give it. If your JSON string is an array, you will have an NSArray. If your JSON data is a dictionary, you will get an NSDictionary.
Convert your JSON data (your data variable) to string and print it out with NSLog. To convert NSData to NSString, use something like:
NSString *myString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];
If you print it out and see a JSON array, you simply don't have a dictionary there.. If you can alter the server-code that generates the JSON, you may be able to change that.
One more thing I noticed, you assume that the returning container is mutable. If I'm not mistaken, you need to use an option like NSJSONReadingMutableContainers in the options parameter of JSONObjectWithData to get that.
One last tip, if you want to check in code if you have an NSArray (or NSDictionary), use something like:
if ([obj isKindOfClass:[NSArray class]]) {...}