Leaflet,js - how do I create the JSON file?

Camillia
Camillia used Ask the Experts™
on
I've used Leaflet.js before...at my last job. We had a JSON file that held the longitude and latitude. It was generated using C# code.

I'm following this example https://asmaloney.com/2014/01/code/creating-an-interactive-map-with-leaflet-and-openstreetmap/

In our database, we have a list of addresses. How can I generate a JSON file of those addresses? In the link above, it's in "The JSON Data" section of the site. How is that file generated?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Not sure what server side language you're using, so I can't give you specifics but the JSON file is just an array of objects - each object having 4 properties - name, url, lat, lng. You would need to query the database and convert the results to JSON. In PHP, a simple example might look something like this::

<?php
$db = new PDO("mysql:host=localhost;dbname=yourDb", "username", "password");
$query = $db->query("SELECT name, url, lat, lng FROM yourTable");
$rows = $query->fetchAll(PDO::FETCH_OBJ);
echo json_encode($rows);

Open in new window

You would need to query the database and convert the results to JSON
This should get me going. Let me take a closer look.

I'm using C#
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
OK. The principle remains the same. You may want to install JSON.NET (available through nuget). Once you've queries you database and have an array of objects, then you'd serialize it:

var json = JsonConvert.SerializeObject(yourObjectArray);
Thanks, chris. I'll work on it tonight. Thanks for helping me out.
thanks :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial