Google Geocoding API. I have a problem with parsing json response - iphone

I have a problem with parsing json response. My error is: [__NSArrayI objectForKey:]: unrecognized selector sent to instance. As I see the params should be NSDictionary. Why does the params is an NSArray?
enter code hereNSDictionary *dictionary = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
NSString* requestStatus = [dictionary objectForKeyNotNull:#"status"];
if ([requestStatus isEqualToString:#"OK"])
{
NSDictionary *params = [dictionary objectForKey:#"results"];
NSString *addresses = [params objectForKey:#"formatted_address"];
}`enter code here`
My json is :
{
"status": "OK",
"results": [ {
"types": [ "street_address" ],
"formatted_address": "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"address_components": [ {
"long_name": "1600",
"short_name": "1600",
"types": [ "street_number" ]
}, {
"long_name": "Amphitheatre Pkwy",
"short_name": "Amphitheatre Pkwy",
"types": [ "route" ]
}, {
"long_name": "Mountain View",
"short_name": "Mountain View",
"types": [ "locality", "political" ]
}, {
"long_name": "California",
"short_name": "CA",
"types": [ "administrative_area_level_1", "political" ]
}, {
"long_name": "United States",
"short_name": "US",
"types": [ "country", "political" ]
}, {
"long_name": "94043",
"short_name": "94043",
"types": [ "postal_code" ]
} ],
"geometry": {
"location": {
"lat": 37.4219720,
"lng": -122.0841430
},
"location_type": "ROOFTOP",
"viewport": {
"southwest": {
"lat": 37.4188244,
"lng": -122.0872906
},
"northeast": {
"lat": 37.4251196,
"lng": -122.0809954
}
}
}
} ]
}

For #"results" key you have an array.
So you need
NSString *addresses = [[params objectAtIndex:0] objectForKey:#"formatted_address"];

Related

Stuck on simple jolt transformation

So I have this json retrieved from the Google Maps API and I just want to get longitude and latitude. I am looking to use the jolt template to extract just the information that I need.
{
"results": [
{
"address_components": [
{
"long_name": "1115",
"short_name": "1115",
"types": [
"street_number"
]
},
{
"long_name": "West Idaho Avenue",
"short_name": "W Idaho Ave",
"types": [
"route"
]
},
{
"long_name": "Ontario",
"short_name": "Ontario",
"types": [
"locality",
"political"
]
},
{
"long_name": "Malheur County",
"short_name": "Malheur County",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Oregon",
"short_name": "OR",
"types": [
"administrative_area_level_1",
"political"
]
},
{`enter code here`
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
},
{
"long_name": "97914",
"short_name": "97914",
"types": [
"postal_code"
]
},
{
"long_name": "2146",
"short_name": "2146",
"types": [
"postal_code_suffix"
]
}
],
"formatted_address": "1115 W Idaho Ave, Ontario, OR 97914, USA",
"geometry": {
"location": {
"lat": 44.0294445,
"lng": -116.9776502
},
"location_type": "ROOFTOP",
"viewport": {
"northeast": {
"lat": 44.03079348029149,
"lng": -116.9763012197085
},
"southwest": {
"lat": 44.02809551970849,
"lng": -116.9789991802915
}
}
},
"partial_match": true,
"place_id": "ChIJP3C3Z6uPr1QRUDkcSIXzx5g",
"types": [
"establishment",
"point_of_interest",
"school"
]
}
],
"status": "OK"
}
So this is the jolt spec that I am using:
[
{
"operation": "shift",
"spec": {
"results": {
"*": {
"geometry": {
"location": {
"lat": "employees[&1].firstName",
"lng": "employees[&1].lastName"
}
}
}
}
}
}
]
I would like to retrieve a json that looks like this:
{
"data" : [
{
"lng": "-116.9763012197085",
"lat": "44.0294445"
} ]
}
But I keep getting null... Any help would be appreciated thanks
Your original spec wasn't working because "lat": "employees[&1].firstName" should be "lat": "employees[&3].firstName".
In this case &1 evaluated to the word "location". &3 gets you up the tree to the index of the input results array, which is what I think you meant.
Shift maintains a stack as it doing its transform, the & wildcard lets you grab previously matched values from the stack / up the tree.
From where "lat" is in the spec, it is 4 levels up the stack 0,1,2,3 to get to the index of the results array, that was matched by the *.
Spec
[
{
"operation": "shift",
"spec": {
"results": {
"*": {
"geometry": {
"location": {
"lat": "data[&3].lat",
"lng": "data[&3].lng"
}
}
}
}
}
}
]

Extracting data from HTTP Response - Angular 5

I am trying to extract lat and lng values form the resulting JSON, from HTTP response -
lat: any;
lng: any;
geoResult: any;
public getGeoCoordinates(store) {
let apiUrl = '/assets/GoogleGeoCoordinates.json';
this.http.get(apiUrl).subscribe( resp => {
this.geoResult = resp;
});
}
from above method, I am trying to capture the values as follows
if (resp['status'] == 'OK' && resp['results']['length'] > 0) {
this.lat = resp['results'][0]['geometry']['location']['lat'];
this.lng = resp['results'][0]['geometry']['location']['lng'];
}
if I use alert(), like alert('Latitude : ' + resp['results'][0]['geometry']['location']['lat']);
alert is showing the value.
If I store that value in 'this.lat' variable, I am getting 'undefined'
can someone help me, to understand to get the values from the json HTTP response
JSON file content - GoogleGeoCoordinates.json
{
"results": [
{
"address_components": [
{
"long_name": "120",
"short_name": "120",
"types": [
"street_number"
]
},
{
"long_name": "West 56th Street",
"short_name": "W 56th St",
"types": [
"route"
]
},
{
"long_name": "Manhattan",
"short_name": "Manhattan",
"types": [
"political",
"sublocality",
"sublocality_level_1"
]
},
{
"long_name": "New York",
"short_name": "New York",
"types": [
"locality",
"political"
]
},
{
"long_name": "New York County",
"short_name": "New York County",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "New York",
"short_name": "NY",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
},
{
"long_name": "10019",
"short_name": "10019",
"types": [
"postal_code"
]
}
],
"formatted_address": "120 W 56th St, New York, NY 10019, USA",
"geometry": {
"location": {
"lat": 40.7640254,
"lng": -73.97896
},
"location_type": "ROOFTOP",
"viewport": {
"northeast": {
"lat": 40.7653743802915,
"lng": -73.97761101970849
},
"southwest": {
"lat": 40.7626764197085,
"lng": -73.98030898029151
}
}
},
"place_id": "ChIJE4YK3PlYwokRybTACneYny4",
"types": [
"street_address"
]
}
],
"status": "OK"
}
I have assigned the values outside the 'subscribe', and it seems started working.
working code as follows -
public getGeoCoordinates(store) {
let apiUrl = '/assets/GoogleGeoCoordinates.json';
this.http.get(apiUrl).subscribe( resp => {
this.geoResult = resp;
});
this.lat = this.geoResult['results'][0]['geometry']['location']['lat'];
this.lng = this.geoResult['results'][0]['geometry']['location']['lng'];
}

How to find all mongoDB documents based on geolocation

I'm using the react geosuggest package to save locations of my events in MongoDB. The package takes data from Google places and each time I store it, it gets stored like this:
"_id": "vvagbSbEginyrQGK8",
"name": "test3",
"googleLocation": {
"label": "Testaccio, Roma, Italia",
"placeId": "ChIJE47T5i5gLxMRhiCxCUAFq4Q",
"isFixture": false,
"gmaps": {
"address_components": [
{
"long_name": "Monte Testaccio",
"short_name": "Monte Testaccio",
"types": [
"establishment",
"natural_feature"
]
},
{
"long_name": "Rome",
"short_name": "Rome",
"types": [
"locality",
"political"
]
},
{
"long_name": "Rome",
"short_name": "Rome",
"types": [
"administrative_area_level_3",
"political"
]
},
{
"long_name": "Metropolitan City of Rome",
"short_name": "RM",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Lazio",
"short_name": "Lazio",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Italia",
"short_name": "IT",
"types": [
"country",
"political"
]
},
{
"long_name": "00153",
"short_name": "00153",
"types": [
"postal_code"
]
}
],
"formatted_address": "Monte Testaccio, 00153 Rome, Italia",
"geometry": {
"location": {},
"location_type": "APPROXIMATE",
"viewport": {
"f": {
"b": 41.87460301970851,
"f": 41.87730098029151
},
"b": {
"b": 12.474345019708494,
"f": 12.477042980291571
}
}
},
"place_id": "ChIJE47T5i5gLxMRhiCxCUAFq4Q",
"types": [
"establishment",
"natural_feature"
]
},
"location": {
"lat": 41.87595200000001,
"lng": 12.475693999999976
}
}
I've tried a lot of different queries with the mongodb near query, but I can't figure it out. Anyone know about a query that will for example find all documents within 10000 meter based on longitude and latitude that I send in.
According to the current version of mongodb, it has Gospatial query operators and you can achieve what you need after modifying the collection which contains the places as following:
First you should create "2dsphere" index for the collection, let's name it "places" as following:
db.places.createIndex( { loc : "2dsphere" } );
/*
the document should contain "loc" property as following:
{
loc : { type: "Point", coordinates: [ -73.88, 40.78 ] },
//+ the other needed properties.
}
*/
Then for a specific origin and distance range, you can apply the following query:
db.places.find(
{
loc:
{ $near :
{
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ]
//the origin point.
},
$minDistance: 1000, //in meter
$maxDistance: 5000 //in meter
}
}
}
)
Note that the coordinates ordered as following [long,lat].
for more information you can check the mongodb document in the following link:
2Dshpere indexes
Geospatial operators

Problems with parsing JSON feed in app

The following JSON feed contains orders.
How can I retrieve each order, when it doesn't contain a key (canĀ“t use objectForKey)? Between the opening bracket and the first curly bracket there is no key.
[ <-----
{ <-----
"person": {
"address": "My road",
"city": "My City",
"name": "My Name",
},
"orderDate": "30.05.2012 10:27",
"orderId": 10001,
"orderlines": [
{
"unit": {
"unitId": "RA005",
"name": "My Unit name",
},
"id": 10007,
"deliveryInfo": {
"count": 1,
"date": "30.05.2012",
"format": "MY_FORMAT",
},
"sum": 0,
"details": "Testing",
"priority": 3,
}
],
"received": true,
"status": "REGISTERED",
"sumPrice": 0
},
{
"person": {
"address": "My road 2",
"city": "My City 2",
"name": "My Name 2",
},
"orderDate": "30.04.2012 10:27",
"orderId": 10002,
"orderlines": [
{
"unit": {
"unitId": "RA006",
"name": "My Unit name 2",
},
"id": 10008,
"deliveryInfo": {
"count": 2,
"date": "01.06.2012",
"format": "MY_FORMAT",
},
"sum": 0,
"details": "Testing",
"priority": 2,
}
],
"received": true,
"status": "REGISTERED",
"sumPrice": 0
}
]
whenever you see [] (square brackets), it represents array. so use object at index to return the at indexes. one approach is to pass the object into array like
NSMutableArray *arr = (NSMutableArray*) jsonData;
since the root of your json is an array,
NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &e];
if (!jsonArray) {
NSLog(#"Error parsing JSON: %#", e);
} else {
for(NSDictionary *item in jsonArray) {
NSLog(#"Item: %#", item);
}
}

Json parsing wrong in IOS

Hi i have some problem parsing the json in IOS. This is the json data.
{
"app_info": [
{
"app_name": "haka",
"sync_protocol_version": "1"
}
],
"updates": [
{
"timestamp": "Sat Apr 21 13:04:08 IST 2012",
"people": [
{
"personal_info": [
{
"first_name": "phlox",
"last_name": "",
"employee_id": "010",
"gender": "-",
"marital_status": "-",
"nationality": "Denobulan",
"dob": "re-23",
"photo": "http://c.cc/users/010/profile/image"
}
],
"contact_details": [
{
"address": [
{
"street": "#1, this way",
"city": "tank",
"state": "sick bay",
"zip": "0978",
"country": "Enterprise"
}
],
"telephone": [
{
"work": "010",
"mobile": "010",
"home": "010"
}
],
"email": [
{
"work": "phlox#nx-10.ent",
"personal": ""
}
]
}
],
"emergency": [
{
"contact": [
{
"name": "-",
"relationship": "",
"telephone": [
{
"work": "",
"home": "",
"mobile": ""
}
]
}
],
"blood_group": ""
}
],
"categorization": [
{
"designation": "",
"department": "",
"location": "",
"joining_date": ""
}
]
},
{
"personal_info": [
{
"first_name": "",
"last_name": "",
"employee_id": "",
"gender": "",
"marital_status": "",
"nationality": "",
"dob": "",
"photo": ""
}
],
"contact_details": [
{
"address": [
{
"street": "",
"city": "",
"state": "",
"zip": "",
"country": ""
}
],
"telephone": [
{
"work": "",
"mobile": "",
"home": ""
}
],
"email": [
{
"work": "",
"personal": ""
}
]
}
],
"emergency": [
{
"contact": [
{
"name": "",
"relationship": "",
"telephone": [
{
"work": "",
"home": "",
"mobile": ""
}
]
}
],
"blood_group": ""
}
],
"categorization": [
{
"designation": "",
"department": "",
"location": "",
"joining_date": ""
}
]
}
],
"messages": [
{
"sender": "Archer<admin#nx-10.ent>",
"sender_role": "admin",
"message_type": "broadcast",
"message": "parking space up for grabs",
"message_recipients": "all",
"reply_permitted": "0"
}
],
"events": [
{
"creator": "Travis<ensign#nx-01.ent>",
"event_title": "",
"event_description": "",
"event_time_start": "",
"event_time_end": "",
"location": "",
"invitees": [
{
"id": "020",
"acceptance": "1"
}
]
}
],
"settings": [
{
"sync_frequency": "0"
}
]
}
]}
This is a valid json format. I have checked it with http://jsonlint.com/ and using http://braincast.nl/samples/jsoneditor/
see the structure of json value.According to structure the people tag should return count of 2, as it has 2 objects, but it is returning only 1 while parsing.
I am completely out of options. Please help guys.
This is the code i am using for parsing
NSString *textPAth = [[NSBundle mainBundle] pathForResource:#"sync" ofType:#"json"];
NSError *error;
NSString *content = [NSString stringWithContentsOfFile:textPAth encoding:NSUTF8StringEncoding error:&error];
NSArray *jsonArray = [[content JSONValue] retain];
NSLog(#"JSON ARRAY %# AND COUNT %d",[[jsonArray valueForKey:#"updates"] valueForKey:#"people"],[[[jsonArray valueForKey:#"updates"] valueForKey:#"people"] count]);
I'm not sure why every dictionary is wrapped with an array, when there is only 1 entry (unnecessary), but if you want to parse it you'd want something like this:
NSDictionary *jsonDict = [[content JSONValue] retain];
NSArray *updatesArray = [jsonDict objectForKey:#"updates"];
NSDictionary *updatesDict = [updatesArray objectAtIndex:0];
NSArray *peopleArray = [updatesDict objectForKey:#"people"];
NSLog(#"People count: %i", [peopleArray count]);
for (NSDictionary *peopleDict in peopleArray) {
//do something
}
As a side note, if this is your JSON, you'll want to remove the [arrays] for entries that are not actually arrays. While you can still parse it, it means you'll have to read the array first then get the dictionary from the objectAtIndex:0, which is totally inefficient and pointless.
Could it be that every one of your objects is wrapped in an array and giving unexpected results?