Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging JSON values using $.each

So I have a JSON feed and i'm simply trying to print out some values.

My Javascript below sort of works. But it doesn't look very 'correct'. Is there a better way of doing this please?

JSON

{
   "info":[
      {
         "lon":-2.1,
         "lat":55.2
      },
      {
         "lon":-2.12,
         "lat":55.23
      }
   ]
}

JavaScript

var jsonURL = "url here";

$.getJSON(jsonURL, function(json1) {
    $.each(json1, function(key, data) {
        $.each(data, function(key, data){

            var latLng = new google.maps.LatLng(
                    data.lat, data.lon);

            var marker = new google.maps.Marker({
                position : latLng
            });

            marker.setMap(map);

        });
    });
});
like image 561
Oliver Evans Avatar asked Dec 15 '14 01:12

Oliver Evans


People also ask

Is JSON good for logging?

JSON logging is the best format for storing your logs, and here's why. Usually, we log application data in a file. But we need a better framework. If we write logs as JSON, we can easily search fields with a JSON key.

What is JSON logging?

The JSON format is ubiquitous and used everywhere from logging in web apps to message passing for microcontrollers. Thanks to its compact nature and it being easily readable by humans, JSON has become the de facto standard for sharing structured data.

How do I enable JSON logging?

To enable parsing JSON log, you add parse: json to a pipeline in the ClusterLogForwarder CR, as shown in the following example. When you enable parsing JSON logs by using parse: json , the CR copies the JSON-structured log entry in a structured field, as shown in the following example.


1 Answers

You know the "info" attribute exists and don't need a loop to get to it. The inner loop looks good.

$.getJSON(jsonURL, function(json1) {

        $.each(json1.info, function(key, data){

            var latLng = new google.maps.LatLng(
                    data.lat, data.lon);

            var marker = new google.maps.Marker({
                position : latLng
            });

            marker.setMap(map);

        });
});
like image 170
Robert Levy Avatar answered Sep 28 '22 04:09

Robert Levy