internal static void AddLayerDetails(GISLayerInfo info, JSONObject obj)
 {
     for (int idx = 0; idx < obj.Count; idx++)
     {
         switch (obj[idx])
         {
             case "id":
                 break;
             case "name":
                 break;
             case "type":
                 info._type = obj.getString("type");
                 break;
             case "geometryType":
                 break;
             case "copyrightText":
             case "parentLayer":
                 //{"id" : <parentLayerId>, "name" : "<parentLayerName>"}
                 break;
             case "subLayers":
             //[    {"id" : <subLayerId1>, "name" : "<subLayerName1>"},    {"id" : <subLayerId2>, "name" : "<subLayerName2>"}],
             case "minScale":
                 break;
             case "maxScale":
                 break;
             case "extent":
                 info._baseExtent = EsriEnvelope.Create(obj.getJSONObject("extent"));
                 break;
             case "displayField":
                 break;
             case "fields":
                 EsriField.ProcessFields(info._Fields, obj.getJSONArray("fields"));
                 info.IsQueryable = true;
                 break;
         }
     }
 }
        private async Task AddServiceFolders(System.Collections.IList responseReader, List<GISService> services)
        {
            for (int idx = 0; idx < responseReader.Count; idx++)
            {
                string requestUrl = String.Format(CATALOG_FOLDER_URL, Server.Host, responseReader[idx], Server.ServletPath);

                try
                {
                    var result = await webClient.GetRequestAsync(requestUrl);

                    if (result.success)
                    {
                        Nii.JSON.JSONObject responseReaderService = new Nii.JSON.JSONObject(result.output);

                        for (int idx2 = 0; idx2 < responseReaderService.Count; idx2++)
                        {
                            switch (responseReaderService[idx2])
                            {
                                case "services":
                                    List<GISService> nservices = EsriServiceResponse.ProcessServiceReturn(responseReaderService.getJSONArray("services").List);
                                    AddServiceValues(_Server._services, nservices);
                                    break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Server.RaiseErrorResponse(GISResponse.ProcessErrorResponse(ex.Message, requestUrl, "service folders"));
                }
            }
        }
        internal void ProcessQueryReturn(object sender, WebEventArgs e)
        {
            GISResponse response = e.UserState as GISResponse;

            try
            {
                Nii.JSON.JSONObject responseReader = new Nii.JSON.JSONObject(e.ResponseString);
                response = EsriFeatureResponse.ProcessFeatureReturn(responseReader.getJSONArray("features"), response as GISFeatureResponse, e.ResponseString);
            }
            catch (Exception ex)
            {
                response.HasError = true;
                response.ErrorMessage = ex.Message;
                response = GISResponse.ProcessErrorResponse(ex.Message, response.LastRequest, e.ResponseString);
            }

            Server.RaiseSearchResponse(response);
        }
        private async Task<bool> ProcessServiceReturn(string responseString)
        {
            GISResponse response = new GISResponse();

            Server._lastUpdated = DateTime.Now;
            _Server._services.Clear();
            // sdfds        http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/6/24/17.jpg

            try
            {
                response.LastResponse = responseString;
                Nii.JSON.JSONObject responseReader = new Nii.JSON.JSONObject(responseString);

                for (int idx = 0; idx < responseReader.Count; idx++)
                {
                    switch (responseReader[idx])
                    {
                        case "error":
                            response = GISResponse.ProcessErrorResponse(responseReader.getString("message"), response.LastRequest, responseString);
                            Server.RaiseErrorResponse(response);
                            break;
                        case "services":
                            List<GISService> nservices = EsriServiceResponse.ProcessServiceReturn(responseReader.getJSONArray("services").List);
                            AddServiceValues(_Server._services, nservices);
                            Server.RaiseServiceResponse();
                            break;
                        case "folders":
                            await AddServiceFolders(responseReader.getJSONArray("folders").List, _Server._services);
                            break;
                    }
                }

                return true;
                //if (UNRESOLVED_SERVICES == 0) Server.RaiseServiceResponse();
            }
            catch (Exception ex)
            {
                response.HasError = true;
                response.ErrorMessage = ex.Message;
                response = GISResponse.ProcessErrorResponse(ex.Message, response.LastRequest, responseString);
                Server.RaiseErrorResponse(response);
                return false;
            }
        }
        void ProcessIdentifyResponse(object sender, WebEventArgs e)
        {
            try
            {
                GISFeatureResponse response = e.UserState as GISFeatureResponse;
                response.LastResponse = e.ResponseString;
                Nii.JSON.JSONObject j = new Nii.JSON.JSONObject(e.ResponseString);

                //{"authenticationResultCode":"ValidCredentials","brandLogoUri":"http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png","copyright":"Copyright © 2010 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.","resourceSets":[{"estimatedTotal":1,"resources":[{"__type":"ImageryMetadata:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1","imageHeight":256,"imageUrl":"http:\/\/t3.tiles.virtualearth.net\/tiles\/a032010110123333.jpeg?g=580&mkt={culture}&token={token}","imageUrlSubdomains":null,"imageWidth":256,"imageryProviders":null,"vintageEnd":"28 Feb 2007 GMT","vintageStart":"01 Jun 2006 GMT","zoomMax":15,"zoomMin":15}]}],"statusCode":200,"statusDescription":"OK","traceId":"986114694e894832aeb8e3c4e53ca7e3|CH1M001465|02.00.147.700|"}            }
                bool hasFields = response.Layers.Count > 0 && response.Layers[0]._Fields.Count > 0;

                Nii.JSON.JSONArray resultObj = j.getJSONArray("results");

                for (int i = 0; i < resultObj.Count; i++)
                {
                    Nii.JSON.JSONObject obj = resultObj[i] as Nii.JSON.JSONObject;

                    GISFeature feature = EsriFeature.Create(obj, response.Layers[0], hasFields);

                    if (feature.Envelope == null) feature.Envelope = response._envelope;

                    response.Features.Add(feature);
                }
                Server.RaiseIdentifyResponse(response);
            }
            catch (Exception ex)
            {
                Server.RaiseErrorResponse(new GISResponse() { _envelope = null, _layers = new List<GISLayerInfo>() { new EsriLayerInfo() { _name = "Search", _type = "Search" } }, ErrorMessage = ex.Message });
            }
        }