如何从Rest URL读取JSON并加载到数组

时间:2017-07-23 06:30:00

标签: c# json

如果我有一个像

这样的休息服务网址,请告诉我
( function( x ){ return x.Parent.Child.bind( x )() } )( myArray );

JSON看起来像

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

如何在C#中设置对服务的请求,并将{"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"} ] } 之后的所有名称加载到名为appData/的数组中?

1 个答案:

答案 0 :(得分:2)

以下单元测试演示

  

如何在C#中设置对服务的请求,并将appData/之后的所有名称加载到名为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; }
    }
}