10

I'm trying to iterate through a GeoJSON file (below) and eventually populate an array with a few attributes within "properties". I keep coming up short working through various examples I can find. Do I need to embed another $.each() to get to properties. If it isn't obvious I'm new to this and I've hit the wall.

What I have so far:

$(document).ready(function () {    
            $.getJSON('testpoint.geojson', function (data) {
                var items = [];
                $.each(data.features, function (key, val) {
                    items.push('<li id="' + key + '">' + val + '</li>');
                });
                $('<ul/>', {
                    'class':'my-new-list',
                    html:items.join('')
                }).appendTo('body');
            });
        });

And my json is as follows:

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "properties": {
                "gid": 1,
                "PID": "9-18-3",
                "BCT": "BCT126",
                "OWNER_TYPE": "A",
                "LOCNO": 0,
                "LOCEXT": "",
                "STREET": "CROSBY LANE",
                "ACQUIRED": "5/7/2010",
                "GRANTOR": "John A. SPARGO",
                "UPLAND": 0,
                "WETLAND": 3.96,
                "TOTAL": 3.96,
                "HABITAT": "salt marsh"
            },
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -70.03209,
                    41.78278
                ]
            }
        }
    ]
}
7

You are almost there. Another .each for val.properties should work:

$.each(data.features, function (key, val) {
    $.each(val.properties, function(i,j){
        items.push('<li id="' + i + '">' + j + '</li>');
    })              
});
  • 2
    Thanks Cyrus, that did the trick. I tried every combo but that. I'm a beginner and iterations can become overwhelming quickly – geomajor56 Jul 19 '12 at 19:07
5

What is written above will loop through each element in each JSON object. Those are JSON objects so you want to treat them as the objects geometry and properties then simply reference them using dot notation.

$.each(data.features, function (key, val) {
  geometry = val.geometry;
  properties = val.properties;
  alert (properties.place);  
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.