UISearchBar and JSON Data - json

This is my first time using a UISearchBar and it is proving to confuse me.
I think my biggest problem here is not understanding how JSON is stored and displayed.
I have my JSON data stored as:
[{
"referralId" : "v-1519014616",
"name" : "Empire State Building",
"storeId" : "",
"hereNow" : {
"count" : 2,
"summary" : "2 people are here",
"groups" : [
{
"count" : 2,
"type" : "others",
"name" : "Other people here",
"items" : [
]
}
]
},
"stats" : {
"tipCount" : 1105,
"checkinsCount" : 185916,
"usersCount" : 129661
},
"venueRatingBlacklisted" : true,
"beenHere" : {
"lastCheckinExpiredAt" : 0
},
"specials" : {
"count" : 0,
"items" : [
]
},
"venuePage" : {
"id" : "64514349"
},
"verified" : true,
"location" : {
"state" : "NY",
"neighborhood" : "Midtown Manhattan, New York, NY",
"crossStreet" : "btwn 33rd & 34th St",
"lat" : 40.748469532965927,
"address" : "350 5th Ave",
"cc" : "US",
"city" : "New York",
"postalCode" : "10118",
"formattedAddress" : [
"350 5th Ave (btwn 33rd & 34th St)",
"New York, NY 10118"
],
"lng" : -73.985513794430119,
"distance" : 17,
"country" : "United States"
},
"hasPerk" : false,
"id" : "43695300f964a5208c291fe3",
"categories" : [
{
... etc
},
I have this stored in an array titled locations and I am trying to filter through it with a search bar.
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let name = self.locations[0]["name"].string
filteredLocations = locations.filter { names in
return (name!.lowercased().contains(searchText.lowercased()))
}
tableView.reloadData()
}
Now, obviously, name is only going to search for the item in the array. How would I be able to filter through every location's name key to properly filter?

try this one ... don't take name first from location array...
let filterArr = locations.filter {
return (($0["name"] as! String).lowercased().contains(searchText.lowercased())))
}
use this one to get the search result

Related

Pulling JSON data with a 3rd party library swift 4

I am using a 3rd party GitHub library in my Xcode folder. Its giving me access to Yelps API where i would like to get search results within my app. I am successfully downloading converting the data into a JSON string.
But i'm stuck on the logic of Decoding the JSON string so that i can access the actual parameters, such as name, display_address and price. I have use "Decode" before, but never through a 3rd party library. I'm sure its possible and i'm probably just a step or two steps away, but my inexperience is impeding that right now. Any help. Thank you
Here is my function to get the JSON data and below is the response i get
func getYelp() {
let yelpAPIClient = CDYelpAPIClient(apiKey: "MY_API_KEY")
yelpAPIClient.cancelAllPendingAPIRequests()
CDYelpFusionKitManager.shared.apiClient = yelpAPIClient
CDYelpFusionKitManager.shared.apiClient.searchBusinesses(byTerm: "Bars",
location: "Hollywood, California",
latitude: nil,
longitude: nil,
radius: 10000,
categories: [CDYelpBusinessCategoryFilter.bars],
locale: CDYelpLocale.english_unitedStates,
limit: 5,
offset: 0,
sortBy: CDYelpBusinessSortType.distance,
priceTiers: [CDYelpPriceTier.twoDollarSigns],
openNow: true,
openAt: nil,
attributes: nil) { (response) in
if let response = response,
let businesses = response.businesses?.toJSONString(),
businesses.count > 0 {
print(businesses)
}
}
}
Here's the data:
[
{
"name" : "The Know Where Bar",
"coordinates" : {
"latitude" : 34.101480000000002,
"longitude" : -118.3127424
},
"rating" : 4.5,
"price" : "$$",
"review_count" : 170,
"display_phone" : "(323) 871-4108",
"location" : {
"state" : "CA",
"address3" : "",
"address1" : "5634 Hollywood Blvd",
"city" : "Los Angeles",
"display_address" : [
"5634 Hollywood Blvd",
"Los Angeles, CA 90028"
],
"zip_code" : "90028",
"country" : "US"
},
"id" : "KOj4TvBuvRLXlBuG2vrZeQ",
"categories" : [
{
"title" : "Cocktail Bars",
"alias" : "cocktailbars"
},
{
"title" : "Jazz & Blues",
"alias" : "jazzandblues"
},
{
"title" : "Pubs",
"alias" : "pubs"
}
],
"transactions" : [
],
"distance" : 2232.7094070718849,
"phone" : "+13238714108",
"image_url" : "https:\/\/s3-media3.fl.yelpcdn.com\/bphoto\/Ehus0C-bAoA-6fNbwGpSYA\/o.jpg",
"is_closed" : false,
"url" : "https:\/\/www.yelp.com\/biz\/the-know-where-bar-los-angeles?adjust_creative=xVfmfDi4k3F3Jm9zaJU1CA&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=xVfmfDi4k3F3Jm9zaJU1CA"
},
{
"name" : "Lost Property Bar",
"coordinates" : {
"latitude" : 34.1019182,
"longitude" : -118.32650390000001
},
"rating" : 4.5,
"price" : "$$",
"review_count" : 143,
"display_phone" : "(323) 987-4445",
"location" : {
"state" : "CA",
"address3" : "",
"address1" : "1704 N Vine St",
"city" : "Hollywood",
"address2" : "",
"display_address" : [
"1704 N Vine St",
"Hollywood, CA 90028"
],
"zip_code" : "90028",
"country" : "US"
},
"id" : "CBYwPYYEFbpCefJ4rdiyPw",
"categories" : [
{
"title" : "Pubs",
"alias" : "pubs"
},
{
"title" : "Cocktail Bars",
"alias" : "cocktailbars"
}
],
"transactions" : [
],
"distance" : 1145.5564586880064,
"phone" : "+13239874445",
"image_url" : "https:\/\/s3-media1.fl.yelpcdn.com\/bphoto\/pswDPAX_3p73JbPEXwThag\/o.jpg",
"is_closed" : false,
"url" : "https:\/\/www.yelp.com\/biz\/lost-property-bar-hollywood?adjust_creative=xVfmfDi4k3F3Jm9zaJU1CA&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=xVfmfDi4k3F3Jm9zaJU1CA"
},
{
"name" : "Sunset & Vinyl",
"coordinates" : {
"latitude" : 34.098346200000002,
"longitude" : -118.32679779999999
},
"rating" : 4.5,
"price" : "$$",
"review_count" : 42,
"display_phone" : "(424) 646-3375",
"location" : {
"state" : "CA",
"city" : "Los Angeles",
"address1" : "1521 Vine St",
"address2" : "",
"display_address" : [
"1521 Vine St",
"Los Angeles, CA 90028"
],
"zip_code" : "90028",
"country" : "US"
},
"id" : "RKSPAI90ITxbnq1-L_F5NA",
"categories" : [
{
"title" : "Cocktail Bars",
"alias" : "cocktailbars"
},
{
"title" : "Speakeasies",
"alias" : "speakeasies"
},
{
"title" : "Lounges",
"alias" : "lounges"
}
],
"transactions" : [
],
"distance" : 916.67964443671087,
"phone" : "+14246463375",
"image_url" : "https:\/\/s3-media3.fl.yelpcdn.com\/bphoto\/w91DLpKmUGuXTF-zauYAWA\/o.jpg",
"is_closed" : false,
"url" : "https:\/\/www.yelp.com\/biz\/sunset-and-vinyl-los-angeles?adjust_creative=xVfmfDi4k3F3Jm9zaJU1CA&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=xVfmfDi4k3F3Jm9zaJU1CA"
},
{
"name" : "A Simple Bar",
"coordinates" : {
"latitude" : 34.129899999999999,
"longitude" : -118.34882
},
"rating" : 5,
"price" : "$$",
"review_count" : 168,
"display_phone" : "(323) 378-5388",
"location" : {
"state" : "CA",
"address3" : "",
"address1" : "3256 Cahuenga Blvd W",
"city" : "Los Angeles",
"display_address" : [
"3256 Cahuenga Blvd W",
"Los Angeles, CA 90068"
],
"zip_code" : "90068",
"country" : "US"
},
"id" : "qD3-5yqSyPiz-VpJtPsnkg",
"categories" : [
{
"title" : "Bars",
"alias" : "bars"
}
],
"transactions" : [
],
"distance" : 4136.336282884502,
"phone" : "+13233785388",
"image_url" : "https:\/\/s3-media2.fl.yelpcdn.com\/bphoto\/JYcRYDteiyjLh3dSAj7fCg\/o.jpg",
"is_closed" : false,
"url" : "https:\/\/www.yelp.com\/biz\/a-simple-bar-los-angeles?adjust_creative=xVfmfDi4k3F3Jm9zaJU1CA&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=xVfmfDi4k3F3Jm9zaJU1CA"
},
{
"name" : "The Woods",
"coordinates" : {
"latitude" : 34.098860000000002,
"longitude" : -118.34478
},
"rating" : 4,
"price" : "$$",
"review_count" : 456,
"display_phone" : "(323) 876-6612",
"location" : {
"state" : "CA",
"address3" : "",
"address1" : "1533 N La Brea Ave",
"city" : "Hollywood",
"address2" : "",
"display_address" : [
"1533 N La Brea Ave",
"Hollywood, CA 90028"
],
"zip_code" : "90028",
"country" : "US"
},
"id" : "UEim6Xi_sm78yDUpSfI-OQ",
"categories" : [
{
"title" : "Lounges",
"alias" : "lounges"
}
],
"transactions" : [
],
"distance" : 968.92997694605617,
"phone" : "+13238766612",
"image_url" : "https:\/\/s3-media3.fl.yelpcdn.com\/bphoto\/wUQYIlojm2d41ZL38zDMgg\/o.jpg",
"is_closed" : false,
"url" : "https:\/\/www.yelp.com\/biz\/the-woods-hollywood?adjust_creative=xVfmfDi4k3F3Jm9zaJU1CA&utm_campaign=yelp_api_v3&utm_medium=api_v3_business_search&utm_source=xVfmfDi4k3F3Jm9zaJU1CA"
}
]
The 3rd party library already does all the decoding for you. Don't convert the response back to a JSON string.
response.businesses is an array of CDYelpBusiness objects, which are defined like this:
public var id: String?
public var name: String?
public var imageUrl: URL?
public var isClosed: Bool?
public var url: URL?
public var price: String?
public var phone: String?
public var displayPhone: String?
public var photos: [String]?
public var hours: [CDYelpHour]?
public var rating: Double?
public var reviewCount: Int?
public var categories: [CDYelpCategory]?
public var distance: Double?
public var coordinates: CDYelpCoordinates?
public var location: CDYelpLocation?
public var transactions: [String]?
Try iterating over them and accessing their properties like this:
for business in response.businesses {
print(business.name)
}

F# - Compare 2 JsonValue'

I have 2 Json values that are similar, but there are some differences.
Json1:
{
"id": "1",
"people" : [
{
"Id" : 1421,
"Name" : "Jackson",
"Age" : 21,
"Status" : "Available"
},
{
"Id" : 5916,
"Name" : "Steven",
"Age" : 22,
"Status" : "Available"
}
],
"totalRecords" : 2
}
Json2:
{
"id": "1",
"people" : [
{
"Id" : 1421,
"Name" : "Jackson",
"Age" : 21,
"Status" : "Available"
},
{
"Id" : 5916,
"Name" : "Steven",
"Age" : 22,
"Status" : "Unavailable"
},
{
"Id" : 1337,
"Name" : "Alice",
"Age" : 19,
"Status" : "Available"
}
],
"totalRecords" : 3
}
I'd like to know if there's a way to compare the two Jsonvalues. At the moment I de-serialize the data into a type and then use the Id's and the status' to see if anythings changed. I then pick out the parts that are different (In the example it'd be Steven and Alice) and add them to a sequence for later.
I'd like to reverse a few of the steps. I'd like too compare the json, find the differences, deserialize them and then add them to the sequence, or add them to the sequence then de-serialize the whole sequence. Either way, same result.
Any ideas?

How to format the TSV file in Druid

I am trying to load in a TSV in druid using this ingestion speck:
MOST UPDATED SPEC BELOW:
{
"type" : "index",
"spec" : {
"ioConfig" : {
"type" : "index",
"inputSpec" : {
"type": "local",
"baseDir": "quickstart",
"filter": "test_data.json"
}
},
"dataSchema" : {
"dataSource" : "local",
"granularitySpec" : {
"type" : "uniform",
"segmentGranularity" : "hour",
"queryGranularity" : "none",
"intervals" : ["2016-07-18/2016-07-22"]
},
"parser" : {
"type" : "string",
"parseSpec" : {
"format" : "json",
"dimensionsSpec" : {
"dimensions" : ["name", "email", "age"]
},
"timestampSpec" : {
"format" : "yyyy-MM-dd HH:mm:ss",
"column" : "date"
}
}
},
"metricsSpec" : [
{
"name" : "count",
"type" : "count"
},
{
"type" : "doubleSum",
"name" : "age",
"fieldName" : "age"
}
]
}
}
}
If my schema looks like this:
Schema: name email age
And actual dataset looks like this:
name email age Bob Jones 23 Billy Jones 45
Is this how the columns should be formatted^^ in the above dataset for a TSV? Like name email age should be first (the columns) and then the actual data. I am confused how Druid will know how to map the columns to the actual dataset in TSV format.
TSV stands for tab separated format, so it looks the same as csv but you will use tabs instead of commas e.g.
Name<TAB>Age<TAB>Address
Paul<TAB>23<TAB>1115 W Franklin
Bessy the Cow<TAB>5<TAB>Big Farm Way
Zeke<TAB>45<TAB>W Main St
you will use frist line as header to define your column names - so you can use "name", "age" or "email" in dimensions in your spec file
as for the gmt and utc, they are basically the same
There is no time difference between Greenwich Mean Time and
Coordinated Universal Time
first one is time zone, the other one is a time standard
btw don`t forget to include a column with some time value in your tsv file!!
so e.g. if you will have tsv file that looks like:
"name" "position" "office" "age" "start_date" "salary"
"Airi Satou" "Accountant" "Tokyo" "33" "2016-07-16T19:20:30+01:00" "162700"
"Angelica Ramos" "Chief Executive Officer (CEO)" "London" "47" "2016-07-16T19:20:30+01:00" "1200000"
your spec file should look like this:
{
"spec" : {
"ioConfig" : {
"inputSpec" : {
"type": "local",
"baseDir": "path_to_folder",
"filter": "name_of_the_file(s)"
}
},
"dataSchema" : {
"dataSource" : "local",
"granularitySpec" : {
"type" : "uniform",
"segmentGranularity" : "hour",
"queryGranularity" : "none",
"intervals" : ["2016-07-01/2016-07-28"]
},
"parser" : {
"type" : "string",
"parseSpec" : {
"format" : "tsv",
"dimensionsSpec" : {
"dimensions" : [
"position",
"age",
"office"
]
},
"timestampSpec" : {
"format" : "auto",
"column" : "start_date"
}
}
},
"metricsSpec" : [
{
"name" : "count",
"type" : "count"
},
{
"name" : "sum_sallary",
"type" : "longSum",
"fieldName" : "salary"
}
]
}
}
}

Google auto complete places, Omit country name of users location from predictions

I am using google.maps.places.AutocompleteService(); to get query predictions. Is it possible to omit country name from the predictions list of users location. For eaxmple. if i am in united states, when i search for sanfransisco. It should not show United states (Country name) in predictions list. But if i search for location- delhi in india, it should show 'Delhi India'(country name).
Is this possible using this service or any other google api service.
Note that, i need list of suggestion, not something tied with a textfield.
Your help will be greatly appreciated.
This is not possible without manually processing the results. So after receiving the json data as a response, you need to check if the suggested country is the same as the user's country and if it is then do not display it in the textfield.
A JSON response looks like this: In the value field you can check the country and decide whether or not to display it by comparing it to the user's country that needs to be set somewhere in your App.
I believe the JSON response will be save in a List<HashMap<String, String>> So you can loop over it and omit the country.
{
"status": "OK",
"predictions" : [
{
"description" : "Paris, France",
"id" : "691b237b0322f28988f3ce03e321ff72a12167fd",
"matched_substrings" : [
{
"length" : 5,
"offset" : 0
}
],
"place_id" : "ChIJD7fiBh9u5kcRYJSMaMOCCwQ",
"reference" : "CjQlAAAA_KB6EEceSTfkteSSF6U0pvumHCoLUboRcDlAH05N1pZJLmOQbYmboEi0SwXBSoI2EhAhj249tFDCVh4R-PXZkPK8GhTBmp_6_lWljaf1joVs1SH2ttB_tw",
"terms" : [
{
"offset" : 0,
"value" : "Paris"
},
{
"offset" : 7,
"value" : "France"
}
],
"types" : [ "locality", "political", "geocode" ]
},
{
"description" : "Paris Avenue, Earlwood, New South Wales, Australia",
"id" : "359a75f8beff14b1c94f3d42c2aabfac2afbabad",
"matched_substrings" : [
{
"length" : 5,
"offset" : 0
}
],
"place_id" : "ChIJrU3KAHG6EmsR5Uwfrk7azrI",
"reference" : "CkQ2AAAARbzLE-tsSQPgwv8JKBaVtbjY48kInQo9tny0k07FOYb3Z_z_yDTFhQB_Ehpu-IKhvj8Msdb1rJlX7xMr9kfOVRIQVuL4tOtx9L7U8pC0Zx5bLBoUTFbw9R2lTn_EuBayhDvugt8T0Oo",
"terms" : [
{
"offset" : 0,
"value" : "Paris Avenue"
},
{
"offset" : 14,
"value" : "Earlwood"
},
{
"offset" : 24,
"value" : "New South Wales"
},
{
"offset" : 41,
"value" : "Australia"
}
],
...
Reference: https://developers.google.com/places/webservice/autocomplete#location_biasing

How to get JSON into a Freemarker Template (FTL)

I've got a MongoDB which I query and the result I serialize and this string I send to my ftl template. Below is the serialized result:
[
{
"id" : "10",
"title" : "Test Title 1",
"partner" : {
"id" : "1",
"name" : "partner 1 ",
"location" : [{
"locationname" : "locationname 1a",
"city" : ""
},{
"locationname" : "locationname 1b",
"city" : ""
}]
}
},
{
"id" : "6",
"title" : "Test Title 2",
"partner" : {
"id" : "1",
"name" : "partner 2 ",
"location" : [{
"locationname" : "locationname 2b",
"city" : ""
}]
}
}
]
How would I use this in my ftl template?
Thanks for any help.
If you really have to serialize before giving the result to FreeMarker... The JSON syntax for maps and lists happens to be a subset of FTL, so assuming the serialized result is in res, res?eval will give you a list of maps.