Page 1 of 1

How to check if Data Exists on a JSON format inside a localstorage variable

Posted: Wed Feb 04, 2015 10:48 pm
by Poll David

I have a local storage variable that contains a JSON format of Google Places. Question is: How do I fetch the data inside?

Ex. Inside the localstorage is:
code
{"html_attributions":"results":[{"geometry":{"location":{"lat":15.168261,"lng":120.578985}},"name":"Pancake House","place_id":"ChIJnc2qWn_yljMRNKWgoOBq1iQ","types":["cafe"],"vicinity":"Pampanga"}],"status":"OK"}/code

How can I fetch just the place_id?


How to check if Data Exists on a JSON format inside a localstorage variable

Posted: Thu Feb 05, 2015 3:38 am
by Yurii Orishchuk

Hi Poll,

You can use "JSON.parse" to get JS object from this JSON string.

Here is a code:

pre

//Here is you should use your JSON string.
var jsonString = "{}";

var jsObject = JSON.parse(jsonString);

var placeID = "default";

//Access to data if this data exists.
if(jsObject && jsObject.results && jsObject.results[0])
placeID = jsObject.results[0].place_id;

/pre

Regards.


How to check if Data Exists on a JSON format inside a localstorage variable

Posted: Thu Feb 05, 2015 4:03 am
by Poll David

Hi Yurii.

my JSON string is stored in a localstorage variable called "lsvNearbyPlacesJSON"

do i have to do this like:

var jsonString = localStorage.getItem('lsvNearbyPlacesJSON').val();

then follows your code.

////////////////

Follow-up question..

i have a collection that contains places with lat lng as columns..

What i am trying to do is lookup or search inside the JSON string. and If one or two or many of the lat lng on my collection was found inside the JSON result, i would have to place the marker on the map with the lat lng on my collection.

So my the JSON string will be the basis if the places on my collection will be inserted into the map.

Does it make sense?

Hope you can help :(


How to check if Data Exists on a JSON format inside a localstorage variable

Posted: Thu Feb 05, 2015 4:10 pm
by Ihor Didevych

Poll,

var jsonString = localStorage.getItem('lsvNearbyPlacesJSON').val();
Yes, correct.

Follow-up question..
Sorry, not sure I understand you correctly. Please specify your question.


How to check if Data Exists on a JSON format inside a localstorage variable

Posted: Thu Feb 05, 2015 4:28 pm
by Poll David

I have a database collection named ESTABLISHMENTS. inside itare the columns

place_id
visits
lat
lng
vicinity
type

i need to match the place_id values inside the collection if they have a match on the parsed JSON

rephrase:

I just need to lookup or search inside the parsed JSON if any PLACE_ID value from the collection exist inside the JSON string.

if place_id exist in the JSON string, i will place the lat lng of that place_id on the map

hope it makes sense. I really need help


How to check if Data Exists on a JSON format inside a localstorage variable

Posted: Fri Feb 06, 2015 3:21 am
by Yurii Orishchuk

Hi Poll,

Your code is not correct.

Please replace this code:

pre

var jsonString = localStorage.getItem('lsvNearbyPlacesJSON').val();

/pre

With this one:

pre

var jsonString = localStorage.getItem('lsvNearbyPlacesJSON');

/pre

Regards.