0

I have a rest service URL like:

http://domain.ca/ArcGIS/rest/services/appData?f=json&pretty=true

which the JSON looks like

{"currentVersion" : 10.05, 
  "folders" : [], 
  "services" : [
    {"name" : "appData/Drainage", "type" : "MapServer"}, 
    {"name" : "appData/Parks", "type" : "MapServer"}, 
    {"name" : "appData/Planning", "type" : "MapServer"}, 
    {"name" : "appData/QNet", "type" : "MapServer"}, 
    {"name" : "appData/Sanitary", "type" : "MapServer"}, 
    {"name" : "appData/Street_Lights", "type" : "MapServer"}, 
    {"name" : "appData/Survey", "type" : "MapServer"}, 
    {"name" : "appData/Transportation", "type" : "MapServer"}, 
    {"name" : "appData/Water", "type" : "MapServer"}
  ]
}

How can set a request to the service in C# and load all names after appData/ to an array called servicesList ?

1
  • 1
    Can you share the code that you've tried so far? Are you using HttpClient to make the connection or are you using a library like EasyHttp ? Commented Jul 23, 2017 at 6:34

2 Answers 2

2

The following unit test demonstrates

How can set a request to the service in C# and load all names after appData/ to an array called servicesList ?

[TestClass]
public class UnitTest3 {
    public async Task GetServicesList() {
        var url = "http://domain.ca/ArcGIS/rest/services/appData?f=json";
        var client = new HttpClient();
        var response = await client.GetAsync(url);
        var json = await response.Content.ReadAsStringAsync();
        var model = JsonConvert.DeserializeObject<ServiceResponse>(json);
        var servicesList = model.services.Select(s => s.name.Replace("appData/", "")).ToArray();
    }

    public class Service {
        public string name { get; set; }
        public string type { get; set; }
    }

    public class ServiceResponse {
        public double currentVersion { get; set; }
        public IList<object> folders { get; set; }
        public IList<Service> services { get; set; }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks NKosi, but I am getting error on var model = await response.Content.ReadAsAsync<ServiceResponse>(); VS complains the 'HtttpContent' does not cotain a defination for 'ReadAsAsync' and no extension method ....
It's an extension method. You are probably missing a library. I'll update to use Json.Net
It is an extension method found in System.Net.Http.Formatting.dll, v5.2.3.0. It basically replaces the two lines I updated in the answer.
Do I need to import JSON dll here? because I am getting error on JsonConvert now
yes check NuGet for Json.Net. I am surprised it is not already referenced.
|
0

you don't need any classes, just one string of code will extract from a json string the data you neeed

//.... http request code that will  be like the one from @Nkosi answer

var json = await response.Content.ReadAsStringAsync();

string[] services = JObject.Parse(json)["services"]
                    .Select(x => ((string)x["name"]).Substring("appData/".Length))))
                    .ToArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.