jQuery: Looping through some JSON data

trevor1940
trevor1940 used Ask the Experts™
on
Hi
I'm trying to loop through some JSON data on a closed system so I can't upload a sample
Could someone give me a clue where to start?

                $.ajax( {
                                url : link,
                                type : 'get',
                                datatype: json,
                                success : function( resp ) {
                                                console.dir(resp);
                                                var elm;
                                                $.each(resp.features, function(key, value){
                                                                elm.append('<tr />');
                                                                $.each(value.attributes, function(key, value){
                                                                                elm.append('<td>' + key.name + ‘</td>’ …);
                                                                });
                                                });
                                                elm.appendTo('#Results');

                                }
                });

Open in new window


CONSOLE output
displayFieldName: "SORT_NAME"
features:Array[1000]
                0:Object
                attributes:Object
           NAME: "My place"
           LONG_: 123
           LAT: 56
...........
   length: 1000
fieldAliases: Object
      NAME: NAME
      LONG_: LONG
      LAT:LAT

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Rikin ShahMicrosoft Dynamics CRM Consultant

Commented:
Hi,

I suppose, you get the JSON String from where you're sending the request. You just need to convert it to an object and traverse through it.

var obj = JSON.parse(jsonString);

Open in new window

Author

Commented:
Looking at the "Network" tab in Firefox it's already in JSON
Part of the link is "&format=json" so why would I need to parse the jsonString
Isn't success : function( resp ) / resp already a JSON object?

Author

Commented:
I've found some similar looking data here

I'm interested in the feature attributes although different from the sample the format is the same how might I access these?

{  
   "objectIdFieldName":"objectid",
   "globalIdFieldName":"globalid",
   "geometryType":"esriGeometryPoint",
   "spatialReference":{  
      "wkid":4326,
      "latestWkid":4326
   },
   "fields":[  ],
   "features":[  
      {  
         "attributes":{  
            "objectid":271186,
            "facilityid":"174169002093",
            "facname":"CLIFFORD CRONE MIDDLE SCHOOL",
            "factype":null,
            "organiz":null,
            "jurisdict":"NAPERVILLE",
            "pocname":null,
            "pocemail":null,
            "pocphone":null,
            "capacity":"093",
            "hoursoper":null,
            "daysoper":null,
            "accessres":null,
            "opendate":null,
            "closeddate":null,
            "opsstatus":null,
            "lastupdate":null,
            "lasteditor":null,
            "globalid":"{597AEE1C-5E32-42BE-9E4C-4A2A0F6D5F8F}",
            "sheltstat":1,
            "sheltcap":93,
            "sheltcurpop":46,
            "petfriend":0,
            "wheelchair":0,
            "adacomp":0
         },

Open in new window

I've figured it out thus

                $.ajax( {
                                url : link,
                                type : 'get',
                                datatype: json,
                                success : function( resp ) {
//                                                console.dir(resp);
                                                $.each(resp.features, function(i, item){
                                                var elm;
                                                  elm.append('<tr />');
                                                  elm.append('<td>' + item.attributes.name + ‘</td>’ …);
                                                  elm.appendTo('#Results');
                                             });
                                   });

                                }
                });

Open in new window

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