Select to view content in your preferred language

Reading and parsing ... /arcgis/rest/services?f=json

5872
6
Jump to solution
09-07-2011 09:17 AM
DonBarker
Regular Contributor
I'd like to write a Javascript function that reads the ArcGIS Server catalog here

http://129.2.24.163/arcgis/rest/services?f=json

in order to insert the map service names into HTML table rows.

Can someone point me to online help specifically for reading and parsing the JSON object that's returned by the above URL?

Thanks,
0 Kudos
1 Solution

Accepted Solutions
KellyHutchins
Esri Notable Contributor
You can use esri.request to get this information. Here's a sample that gets the info and writes it out to the page.

   function init() {

      
     var  baseURL = "http://129.2.24.163/arcgis/rest/services";
        esri.request({
          url:baseURL, 
          content:{f:"json"},
          callbackParamName:"callback",
          load:getServiceDetails, 
          error:esriConfig.defaults.io.errorHandler
        });
      }
      
    function getServiceDetails(response,args){
      var info = [];
      dojo.forEach(response.folders,function(folder){
        info.push("Folder: " +  folder + "<br/>");        
      });
      dojo.forEach(response.services, function(service){
        info.push("Service Details: " + service.name + " " + service.type + "<br/>");
      });

     dojo.byId('serviceDetails').innerHTML = info.join("");
    }
      dojo.addOnLoad(init);

View solution in original post

0 Kudos
6 Replies
KellyHutchins
Esri Notable Contributor
You can use esri.request to get this information. Here's a sample that gets the info and writes it out to the page.

   function init() {

      
     var  baseURL = "http://129.2.24.163/arcgis/rest/services";
        esri.request({
          url:baseURL, 
          content:{f:"json"},
          callbackParamName:"callback",
          load:getServiceDetails, 
          error:esriConfig.defaults.io.errorHandler
        });
      }
      
    function getServiceDetails(response,args){
      var info = [];
      dojo.forEach(response.folders,function(folder){
        info.push("Folder: " +  folder + "<br/>");        
      });
      dojo.forEach(response.services, function(service){
        info.push("Service Details: " + service.name + " " + service.type + "<br/>");
      });

     dojo.byId('serviceDetails').innerHTML = info.join("");
    }
      dojo.addOnLoad(init);

0 Kudos
DonBarker
Regular Contributor
Works great.  Thanks, Kelly!
0 Kudos
DonBarker
Regular Contributor
Kelly,

How can I fetch and use the other map service attributes that appear in the JSON object, like:

{"serviceDescription":"","mapName":"","description":"","copyrightText":""

I tried
service.description
service.serviceDescription
service.servicedescription
service.mapName
service.mapname

All render "undefined".  I could not find the ref in the API documentation. Can you point the way?

Thanks,
0 Kudos
KellyHutchins
Esri Notable Contributor
Don,

You can get the service details using esri.request with the service url. For example:

      baseURL = "http://129.2.24.163/ArcGIS/rest/services/00_2011_Metadata_Example/MapServer";
        esri.request({
          url:baseURL, 
          content:{f:"json"},
          callbackParamName:"callback",
          load:getLayerDetails, 
          error:esriConfig.defaults.io.errorHandler
        });



In the response that is returned you'll have the service details you see in the rest services directory:
http://129.2.24.163/ArcGIS/rest/services/00_2011_Metadata_Example/MapServer
0 Kudos
DonBarker
Regular Contributor
Sorry I don't quite get it. 

I adapted the code for reading REST catalog (that works ok) to reading the map service attributes.  My code is below.  But nothing writes out to the table cell, not even plain text.  What am I doing wrong?

Thanks,
Don

// P.S. Can someone tell me how to wrap code in my posts?  I saw that in forum help before but can't find that now.  Thanks. //


function init() {
     baseURL = "http://129.2.24.163/ArcGIS/rest/services/00_CRH03/MapServer";
       esri.request({
         url:baseURL,
         content:{f:"json"},
         callbackParamName:"callback",
         load:getLayerDetails,         
         error:esriConfig.defaults.io.errorHandler
       });
     }
   function getLayerDetails(response,args){
     var info = [];    
     dojo.forEach(response.services, function(service){
       info.push(
       "<tr><td>" + service.serviceDescription + "</td></tr>"); 
     });
    dojo.byId('mapServiceTable').innerHTML = info.join("");
   }
     dojo.addOnLoad(init);
</script>
  </head>
  <body>

<table id="mapServiceTable">
</table>
  </body>
0 Kudos
JeffPace
MVP Alum
Your problem is the esri.request returns A service (not services), so your for Loop has nothing to loop over (its a json response, not an array).

try

function getLayerDetails(response,args){
alert(response.serviceDescription);
}

or just alert your response to see what it contains.
0 Kudos