2016-07-01 57 views
2

我有一些代碼使用RestSharp格式化JSON請求來訪問ESRI地理編碼API。代碼有效,但是我想知道是否有更好的方法可以將請求轉換爲正確的格式,下面是我的示例,下面是示例請求的示例。格式化ESRI geocoder的RestSharp請求

request = new RestRequest("geocodeAddresses", Method.POST); 
     request.AddHeader("Content-Type", "application/x-www-form-urlencoded"); 

     //Format the request properly 
     var attributes = new Dictionary<string, object>(); 
     attributes.Add("OBJECTID", address.Address); 
     attributes.Add("Address", address.Address); 
     attributes.Add("City", address.City); 
     attributes.Add("Region", address.State); 
     attributes.Add("Postal", address.ZipCode); 

     JsonObject attributesObj = new JsonObject(); 
     foreach (var parms in attributes) 
     { 
      attributesObj.Add(parms); 
     } 


     JsonObject recordsObj = new JsonObject(); 
     recordsObj.Add("attributes", attributesObj); 
     JsonArray EsriRequest = new JsonArray(); 
     EsriRequest.Add(recordsObj); 
     JsonObject addressObj = new JsonObject(); 
     addressObj.Add("records", EsriRequest); 




     request.AddParameter("addresses", 
      addressObj.ToString()); 
     request.AddParameter("token", esriToken.ToString()); 
     request.AddParameter("f", "json"); 
     request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }; 
     IRestResponse<EsriAddress> responseData = client.Execute<EsriAddress>(request); 

請求輸出樣本:

http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/geocodeAddresses?addresses={"records":[{"attributes":{"OBJECTID":1,"Address":"380 New York St.","City":"Redlands","Region":"CA","Postal":"92373"}},{"attributes":{"OBJECTID":2,"Address":"1 World Way","City":"Los Angeles","Region":"CA","Postal":"90045"}}]}&sourceCountry=USA&token=<YOUR TOKEN>&f=pjson 

我目前只會將一個地址,但在理論上的API可以採取更多然後一次一個。

+0

這是唯一的方法嗎? –

回答

1

下面是我如何向ArcGIS REST API中的geocodeAddresses方法發送一批地址。我沒有使用RestSharp,只是HttpClient

string token = GetToken(); 
string url = "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/geocodeAddresses"; 

using (var client = new HttpClient()) 
{ 
    using (var content = new MultipartFormDataContent()) 
    { 
     var values = new[] 
     { 
      new KeyValuePair<string, string>("token", token), 
      new KeyValuePair<string, string>("forStorage", "true"), 
      new KeyValuePair<string, string>("MaxBatchSize", "1000"), 
      new KeyValuePair<string, string>("outFields", "*"), 
      new KeyValuePair<string, string>("f", "json"), 
      new KeyValuePair<string, string>("addresses", inputJson) // json string containing an array of a complex type 
     }; 

     foreach (var keyValuePair in values)   
      content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key); 

     var response = await client.PostAsync(url, content); 
     Task<string> responseString = response.Content.ReadAsStringAsync(); 
     string outputJson = await responseString; 
    } 
} 
+1

我回來了這個項目,我認爲這是正確的答案,但是你知道如果使用這種批處理方法成本信用? –

+0

對於需要此功能的其他人來說,這是有效的。還不確定是否花費信用。 –

+1

@TravisJ - 如果將「forStorage」參數設置爲true,則會消耗積分。如果您只是通過地理編碼在地圖上顯示點並且不存儲輸出數據,則可以將此參數設置爲false,並且不會收取積分。以下是一些[有關Esri ArcGIS JS API中免費與付費操作的更多信息。](https://developers.arcgis.com/rest/geocode/api-reference/geocoding-free-vs-paid.htm)。僅供參考 - 地理編碼消耗[每1000個地址40個積分進行地理編碼。](http://www.esri.com/software/arcgis/arcgisonline/credits) –